简体   繁体   中英

React Auth Router v6

I am trying to build a simple react app with authentication logic.. the repository can be found here

I am facing issues trying to get to /reports as the app keeps redirecting me to /login page even if the useEffect hook runs and authenticates the user..

What could be missing ?

Thanks

I think your return from PrivateRoute should be changed.
In the beginning isAuthenticated is false and loading is true

isAuthenticated && !loading === false

So your <Navigate replace to="/login" /> is returned.

Instead you can move loading state somewhere above to have it like this

function PrivateRoute({ children }) {
  /* ... */

  if (loading) {
    return <SomeSpinner />
  }

  return isAuthenticated
    ? children
    : <Navigate replace to="/login" />;
}

And you will not have the redirect immediately, but will wait for loading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM