question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[v6] Redirect after finishing authentication

See original GitHub issue

Version

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

  1. Go to /test by clicking on Test-Auth link without authentication. You will be redirected to the Login page.
  2. 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:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
myNameIsDucommented, Jun 20, 2021

@mikunpham I think this is the reason for React. Check out these two examples: react-router5:

class Children extends React.Component {
    componentDidMount() {
        console.log('didmount');
    }

    render() {
        return <div>children</div>;
    }
}

function App() {
    const [state, setState] = React.useState(0);
    const onClick = () => {
        setTimeout(() => {
            setState(v => ++v);
            console.log('push');
        });
    };

    return (
        <>
            <button onClick={onClick}>test btn</button>
            {state > 0 ? <Children /> : null}
        </>
    );
}

react-router6:

function Children() {
    React.useEffect(() => {
        console.log('children');
    }, []);

    return <div>children</div>;
}

function App() {
    const [state, setState] = React.useState(0);
    const onClick = () => {
        setTimeout(() => {
            setState(v => ++v);
            console.log('push');
        });
    };

    return (
        <>
            <button onClick={onClick}>test btn</button>
            {state > 0 ? <Children /> : null}
        </>
    );
}

You can see that the execution order of the two codes is different

0reactions
mikunphamcommented, Jun 22, 2021

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 just setTimeout(() => navigate(URL), 0) as a temporary fix and still looking forward to an alternative solution for this case.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found