gpt4 book ai didi

reactjs - react redux firebase中的私有(private)路由不起作用

转载 作者:行者123 更新时间:2023-12-04 09:36:36 27 4
gpt4 key购买 nike

我一直在尝试将 react-redux-firebase 与我的 react 应用程序集成。除了 protected 路线外,一切正常
我从 react-react-firebase 复制了私有(private)路由的代码。
这是我的私有(private)路线的代码

import React from "react";
import { Route, Redirect } from "react-router-dom";
import { useSelector } from "react-redux";
import { isLoaded, isEmpty } from "react-redux-firebase";

// A wrapper for <Route> that redirects to the login
// screen if you're not yet authenticated or if auth is not
// yet loaded
function PrivateRoute({ children, ...rest }) {
// FIXME: #27 Imp This is not working for some reason, you can still access the not accessible locations
const auth = useSelector((state) => state.firebase.auth);
console.log(auth);
console.log("Empty?");
console.log(isEmpty(auth));
console.log("Load?");
console.log(isLoaded(auth));
return (
<Route
{...rest}
render={({ location }) =>
// FIXME: Here it even if it is false it is rendering the children for some reason
isLoaded(auth) && !isEmpty(auth) ? (
// auth.uid==undefined ? ( // Even this does not work
children
) : (
<Redirect
to={{
pathname: "/redirect/login",
state: { from: location },
}}
/>
)
}
/>
);
}

export default PrivateRoute;
即使 isEmpty 为假,它也会返回 child 。
这是我的 reducer 代码。
import { combineReducers } from "redux";
import { firebaseReducer } from "react-redux-firebase";

export default combineReducers({
firebase: firebaseReducer,

// authReducer,
// apiStatusReducer,
});
在过去的 1 周里,我一直在尝试解决它,并且希望得到任何关于它为什么不起作用的帮助或提示。谢谢您的帮助。
编辑:-
出于某种奇怪的原因,即使交换 child 和重定向位置也有效。
<Route
{...rest}
render={({ location }) =>
// FIXME: Here it even if it is false it is rendering the children for some reason
isLoaded(auth) && isEmpty(auth) ? (
// auth.uid==undefined ? ( // Even this does not work
<Redirect
to={{
pathname: "/redirect/login",
state: { from: location },
}}
/>
) : (
children
)
}
/>

最佳答案

截至 2020 年 9 月 16 日,这是 react-redux-firebase 存储库 (Link to Issue) 中的一个未解决问题。
有一个丑陋的解决方法对于@zachlyoung 提供的这个问题
通常,我们会以这种方式登录:

firebase.login(credentials).then(() => {
props.history.push("/dashboard"); // Private Route
});
我们没有直接进入私有(private)路由,而是再次更新 a​​uth。
firebase.login(credentials).then(() => {
firebase.reloadAuth().then(() => {
props.history.push("/dashboard"); // Private Route
});
});

关于reactjs - react redux firebase中的私有(private)路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62554778/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com