我正在尝试在 React-Ionic 应用程序中设置 Firebase 身份验证上下文,其中包含一些 protected 路由,这些路由未经身份验证就不应访问。到目前为止,它还算有效,但还不是完全有效。我可以毫无问题地登录并使用 protected 路由导航页面,但是一旦刷新页面,它就会返回到登录页面,就好像我没有经过身份验证一样。
我很确定我在某个地方搞砸了,但无法检测到在哪里(实际上有可能整个事情都被错误地实现了,因为我对此很陌生并且仍在学习)。
这是我的文件到目前为止的样子(没有导入和其他东西,只有核心部分):
App.tsx:
const App: React.FC = () => {
const authContext = useContext(AuthContext);
useEffect(() => {
console.log(authContext) **//**
}, [])
return (
<IonApp>
<IonReactRouter>
<IonRouterOutlet>
<Route exact path="/" component={PageLogin} />
<ProtectedRoute path="/secure/home" component={PageHome} isAuthenticated={authContext.authenticated!}/>
<ProtectedRoute path="/secure/lot/:lotId" component={PageLotOverview} isAuthenticated={authContext.authenticated!}/>
<ProtectedRoute path="/secure/shipment/:shipmentId" component={PageShipmentOverview} isAuthenticated={authContext.authenticated!}/>
</IonRouterOutlet>
</IonReactRouter>
</IonApp>
)
}
AuthProvider.tsx:
export const AuthContext = React.createContext<Partial<ContextProps>>({});
export const AuthProvider = ({ children }: any) => {
const [user, setUser] = useState(null as firebase.User | null);
const [loadingAuthState, setLoadingAuthState] = useState(true);
useEffect(() => {
firebase.auth().onAuthStateChanged((user: any) => {
setUser(user);
setLoadingAuthState(false);
});
}, []);
return (
<AuthContext.Provider
value={{
user,
authenticated: user !== null,
setUser,
loadingAuthState
}}>
{children}
</AuthContext.Provider>
);
}
ProtectedRoute.tsx:
interface ProtectedRouteProps extends RouteProps {
component: React.ComponentType<any>;
path: string;
isAuthenticated: boolean
}
export const ProtectedRoute: React.FC<ProtectedRouteProps> = ({ component: Component, path, isAuthenticated }) => {
useEffect(() => {
console.log("from protected ROUTE", isAuthenticated)
}, [])
return (
<Route path={path} render={() => isAuthenticated ? <Component /> : <Redirect to="/" />} />
);
};
index.tsx:
ReactDOM.render(
<AuthProvider>
<App />
</AuthProvider>
, document.getElementById('root'));
如果我可以提供更多有用的信息,请告诉我。预先感谢您的帮助。
最佳答案
第一:每个人都在学习。
第二:有一个非常简单的解决方案,我个人使用它(效果很好)。您可以使用称为“ session 存储”的工具。
从 Firebase 获取用户数据后,请立即执行以下操作:
// When login is successful
window.sessionStorage.setItem('isLogged', 'true');
// This will create a variable with value *true* that
can be accessed from any page within your website
session 存储就像一个本地数据库,可以存储您想要保存的任意数量的值,并且不会在刷新时被删除。然而它只是暂时的,当用户关闭浏览器窗口时将会被删除。
现在,您可以在每个 protected 路线上执行此操作。
useEffect(() => {
if(window.sessionStorage.getItem('isLogged') == 'false')
{
window.location.href = '/login' // send the user to login page
}
}, [])
专业提示:
用户可以使用此机制为您正在开发的任何内容实现“记住我”选项。在这种情况下,您可能必须使用与 sessionStorage
类似的 LocalStorage
,但即使用户关闭浏览器窗口,它也不会删除数据。
同样,当用户访问您的页面时,它会自动将用户重定向到主页而无需登录(如果您创建 useEffect
来检查这一点)。
另一件事:localStorage
和 sessionStorage
对于不同的用户来说是不同的。您或除我之外的任何人都看不到存储在我的浏览器的 localStorage
或 sessionStorage
中的值。
就是这样
关于reactjs - 使用 Firebase Auth Context 在 React-Ionic 应用程序中保护路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64168998/