I would like to access a react protected route when logged in. When I use a button to do so, it works but I can’t access the route using the URL.
Here is how it works :
If a user is connected : he can access his account page using a button in the header (or using the URL)
If a user is not connected : he gets redirected to auth0 login page and then returns to ‘/user’ route.
Here is the issue :
After connection, the user gets redirected to ‘/user’ route but the home page is rendered. Whereas if I use the button in the header, it works perfectly fine.
if the user uses this link, he can go /user route as expected if he is connected.
I’m new in using Auth0 and manipulating authentication. I have no clue how to solve it.
The app is redirecting to the home page after login because of window.location.origin being set as the redirect_uri in authorizationParams. You can try setting the redirect_uri to the /users route. Alternatively, you can try the below adjustment to the AuthenticationGuard code.
The withAuthenticationRequired takes the component you want to protect and a configuration object to customize the auth flow. You are already using the onRedirecting property. There’s another property you can use here: returnTo. This property lets you specify a path for React to redirect a user after the login transaction that the user triggered in this component completes.
(sorry for all the posts deleted I could remember how to add long portions of code)
Thank you very much for the time you spent to reply.
Unfortunately, it didn’t fix my problem. But I found another answer which led me to add the onRedirectCallback prop it almost fixed it.
Actually, I can now be redirected to my /user route but I seems that auth0 goes first to /user then goes to root route but holding some code and state property in URL.
Like so : /?code=[value]&state=[value]%3D%3D.
And then it goes back to /user route.
Can you confirm that you are now being redirected to /users after login?
Regarding your question about the code=[value], this is expected. This is the response received after making a successful /authorize request. Please see this link for more info on this: Add Login Using the Authorization Code Flow with PKCE
Hi,
yes I am redirected to ‘/user’ after I login.
I added {replace: true} in theonRedirectCallback props like this so the root path doesn’t stack anymore: