Firebase身份验证和React受保护的路由 – 仅不断重定向

huangapple go评论80阅读模式
英文:

Firebase auth and React protected route - just keeps redirecting

问题

I'm not sure where the error in my logic is? Apologies in advance for nooby question, am new to React and Firebase.

我的逻辑出错在哪里?提前为新手问题道歉,我刚接触React和Firebase。

I have a sign-in page followed by a profile page to be protected. My logic in the PrivateRoute is that: If the user is authenticated, the authorised will be set to true and so the user will be directed to the profile page. And otherwise, the user will be directed back to the SignIn page.

我有一个登录页面,然后是一个需要保护的个人资料页面。PrivateRoute中的逻辑是:如果用户已经认证,authorized 将被设置为true,用户将被重定向到个人资料页面。否则,用户将被重定向回SignIn页面。

My issue is that even when the user is signed in successfully, the user is unable to go to /profile and keeps getting redirected to /sign-in. Why is that?

我的问题是,即使用户成功登录,用户仍然无法访问/profile,并一直被重定向到/sign-in。为什么会这样?

Code for Protected route:

受保护路由的代码:

import { getAuth, onAuthStateChanged } from "firebase/auth";
import { Navigate, Outlet } from "react-router-dom";
import { useState, useEffect } from "react";

const PrivateRoute = () => {
    const auth = getAuth();
    const [authorised, setAuthorised] = useState(false);
    onAuthStateChanged(auth, (user) => {
        if (user) {
            setAuthorised(true);
        }
        if (!user) {
            setAuthorised(false);
        }
    });
    return(
        authorised ? <Outlet/> : <Navigate to="/sign-in"/>
    )
}

export default PrivateRoute;

Code for App.js

App.js的代码:

import { Route, Routes } from "react-router-dom";
import Profile from "./pages/Profile";

function App() {
  return (
    <div>
      <Route path="/sign-in" element={<SignIn />} />

      <Route element={<PrivateRoute />}> 
        <Route path="/profile" element={<Profile />} />
      </Route>
  );
}

Appreciate any help, and thank you in advance.

感谢任何帮助,提前谢谢。

英文:

I'm not sure where the error in my logic is? Apologies in advance for nooby question, am new to React and Firebase. <br>
I have a sign-in page followed by a profile page to be protected. My logic in the PrivateRoute is that: If the user is authenticated, the authorised will be set to true and so the user will be directed to the profile page. And otherwise, the user will be directed back to the SignIn page.<br>
My issue is that even when the user is signed in successfully, the user is unable to go to /profile and keeps getting redirected to /sign-in. Why is that?

Code for Protected route:

import { getAuth, onAuthStateChanged } from &quot;firebase/auth&quot;;
import { Navigate, Outlet } from &quot;react-router-dom&quot;;
import { useState, useEffect } from &quot;react&quot;;

const PrivateRoute = () =&gt; {
    const auth = getAuth();
    const [authorised, setAuthorised] = useState(false);
    onAuthStateChanged(auth, (user) =&gt; {
        if (user) {
            setAuthorised(true);
        }
        if (!user) {
            setAuthorised(false);
        }
    });
    return(
        authorised ? &lt;Outlet/&gt; : &lt;Navigate to=&quot;/sign-in&quot;/&gt;
    )
}

export default PrivateRoute;

Code for App.js

import { Route, Routes } from &quot;react-router-dom&quot;;
import Profile from &quot;./pages/Profile&quot;

function App() {
  return (
    &lt;div&gt;
      &lt;Route path=&quot;/sign-in&quot; element={&lt;SignIn /&gt;} /&gt;

      &lt;Route element={&lt;PrivateRoute /&gt;}&gt; 
        &lt;Route path=&quot;/profile&quot; element={&lt;Profile /&gt;} /&gt;
      &lt;/Route&gt;
  );
}

Appreciate any help, and thank you in advance

答案1

得分: 1

我认为这是因为在挂载时authorised为假,所以它重定向到sign-in。在你的代码中,PrivateRoute与profile一起工作,但与sign-in不起作用。这意味着在重定向到sign-in后,即使用户已经登录,也无法返回到profile

请参考 https://stackoverflow.com/questions/71703922/react-router-dom-unable-to-render-page-but-routes-back-due-to-privateroute 获取更多详细信息。

英文:

I think it is because authorised is false on mount, so it redirects to sign-in. In your code, PrivateRoute is working with profile and not working with sign-in. It means after redirects to sign-in, you can't go back to profile even user has been signed in.

Please refer https://stackoverflow.com/questions/71703922/react-router-dom-unable-to-render-page-but-routes-back-due-to-privateroute for more details.

答案2

得分: 0

由于onAuthStateChanged是异步的,您的useState可能会在从Firebase接收任何信息之前进行检查。我建议您使用react-firebase-hooks并利用useAuthState函数。

const PrivateRoute = () => {
  const auth = getAuth();
  const [user, loading] = useAuthState(auth);

  if (loading) {
    return "加载中";
  }
  return user ? <Outlet /> : <Navigate to="/sign-in" />;
}
英文:

Because onAuthStateChanged is asynchronous, your useState may be checking before receiving any information from Firebase. I suggest you use react-firebase-hooks and take advantage of the useAuthState function.

const PrivateRoute = () =&gt; {
const auth = getAuth();
const [user, loading] = useAuthState(auth);

if (loading) {
    return &quot;Loading&quot;
}
return user ? &lt;Outlet /&gt; : &lt;Navigate to=&quot;/sign-in&quot; /&gt;

}

huangapple
  • 本文由 发表于 2023年5月22日 00:37:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定