简体   繁体   English

反应身份验证路由器 v6

[英]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..我在尝试访问/reports遇到了问题,因为即使 useEffect 挂钩运行并验证用户身份,应用程序也会不断将我重定向到/login页面。

What could be missing ?可能缺少什么?

Thanks谢谢

I think your return from PrivateRoute should be changed.我认为你从PrivateRoute的回报应该改变。
In the beginning isAuthenticated is false and loading is true一开始isAuthenticated为假, loading为真

isAuthenticated && !loading === false

So your <Navigate replace to="/login" /> is returned.所以你的<Navigate replace to="/login" />被返回。

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而且您不会立即进行重定向,而是会等待加载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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