英文:
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 "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
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
答案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 = () => {
const auth = getAuth();
const [user, loading] = useAuthState(auth);
if (loading) {
return "Loading"
}
return user ? <Outlet /> : <Navigate to="/sign-in" />
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论