[v6] Redirect after finishing authentication
See original GitHub issueVersion
React-router v6
Test Case
https://codesandbox.io/s/react-router-v6-auth-x1onb
Problem
Hi folks,
I tried to mimic this Redirects (Auth) example of react-router@5
into react-router@6
.
There is a problem after users finished the authentication. Instead of redirecting users base on the function navigate
after logging in successfully.
let login = () => {
auth.signin(() => {
navigate(from, { replace: true });
});
};
The page always redirects you to the default protected due to this line of codes.
<Route
{...rest}
element={!auth.user ? element : <Navigate to="/protected" replace />}
/>
I doubt that this happens since my auth state has changed in the <PublicRoute>
component, so it re-rendered again and the navigate
in login callback gets overwritten by another navigate
in <Navigate>
component.
Steps to reproduce
- Go to
/test
by clicking onTest-Auth
link without authentication. You will be redirected to the Login page. - After clicking the Login button -> will be Redirected to
/protected
Expected Behavior
After logging in, you should be redirected to /test
, which is the previous page you intended to go in from the beginning.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top Results From Across the Web
how to redirect in react v6 - Stack Overflow
For example, the user enters www.eCart.com/Cart as the user is not authenticated, it will navigate to the login page www.eCart.com/login.
Read more >[v6] Redirect after finishing authentication · Issue #7879 - GitHub
Go to /test by clicking on Test-Auth link without authentication. You will be redirected to the Login page. · After clicking the Login...
Read more >Redirect After Login with React Router v6 - YouTube
In this video I show you how to redirect users to the page they were trying to go to before they logged in....
Read more >How to Redirect a User After Login in React - MakeUseOf
In React Router v6, there are two ways you can use to redirect a user — the Navigate component and the useNavigate() hook....
Read more >Complete guide to authentication with React Router v6
Learn how to handle user authentication with React Router v6 and gain a better overall understanding of how React Router works.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@mikunpham I think this is the reason for React. Check out these two examples: react-router5:
react-router6:
You can see that the execution order of the two codes is different
Thank you @myNameIsDu
Do you think that the problem comes from changing
<Redirect />
class component to<Navigate />
function component cause this issue? For now, I justsetTimeout(() => navigate(URL), 0)
as a temporary fix and still looking forward to an alternative solution for this case.