Reactjs react redux应用程序中的受保护路由,react路由器未按预期工作
我有passport策略,在后端使用express server。我已经设置了所有的passport策略和身份验证。现在我需要保护我的一些路线。我正在使用redux来存储用户是否登录的状态 我已尝试根据此创建一个专用路由。但它进入了无限循环(组件的无限渲染) 下面的代码成功地连接到redux应用商店,发送了一个操作,后端也正常工作 但请告诉我为什么我会进入内循环 我的受保护函数如下所示:Reactjs react redux应用程序中的受保护路由,react路由器未按预期工作,reactjs,authentication,redux,react-router,react-router-v4,Reactjs,Authentication,Redux,React Router,React Router V4,我有passport策略,在后端使用express server。我已经设置了所有的passport策略和身份验证。现在我需要保护我的一些路线。我正在使用redux来存储用户是否登录的状态 我已尝试根据此创建一个专用路由。但它进入了无限循环(组件的无限渲染) 下面的代码成功地连接到redux应用商店,发送了一个操作,后端也正常工作 但请告诉我为什么我会进入内循环 我的受保护函数如下所示: const login = { type: "Login" }; const logout = {
const login = {
type: "Login"
};
const logout = {
type: "Logout"
};
const AUTH =
process.env.NODE_ENV === "production"
? "https://birdiez.herokuapp.com/auth/check"
: "http://localhost:3090/auth/check";
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={props => {
console.log("calling api");
fetch(AUTH)
.then(response => {
return response.json();
})
.then(data => {
console.log("Promise resolved:", data);
data.msg === "OK" ? store.dispatch(login) : store.dispatch(login);
});
const { status } = store.getState();
return status === true ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: "/",
state: { from: props.location }
}}
/>
);
}}
/>
);
export default PrivateRoute;
class App extends Component {
render() {
return (
<Router>
<Switch>
<Route exact path="/" component={Home} />
<PrivateRoute path="/dash" component={Dash} />
<Route exact path="*" component={Home} />
</Switch>
</Router>
);
}
}
const登录={
类型:“登录”
};
常量注销={
类型:“注销”
};
常数认证=
process.env.NODE_env==“生产”
? "https://birdiez.herokuapp.com/auth/check"
: "http://localhost:3090/auth/check";
const privaterout=({component:component,…rest})=>(
{
log(“调用api”);
获取(授权)
。然后(响应=>{
返回response.json();
})
。然后(数据=>{
日志(“承诺已解决:”,数据);
data.msg==“确定”?store.dispatch(登录):store.dispatch(登录);
});
const{status}=store.getState();
返回状态===真(
) : (
);
}}
/>
);
导出默认私有路由;
我的App.js看起来像:
const login = {
type: "Login"
};
const logout = {
type: "Logout"
};
const AUTH =
process.env.NODE_ENV === "production"
? "https://birdiez.herokuapp.com/auth/check"
: "http://localhost:3090/auth/check";
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={props => {
console.log("calling api");
fetch(AUTH)
.then(response => {
return response.json();
})
.then(data => {
console.log("Promise resolved:", data);
data.msg === "OK" ? store.dispatch(login) : store.dispatch(login);
});
const { status } = store.getState();
return status === true ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: "/",
state: { from: props.location }
}}
/>
);
}}
/>
);
export default PrivateRoute;
class App extends Component {
render() {
return (
<Router>
<Switch>
<Route exact path="/" component={Home} />
<PrivateRoute path="/dash" component={Dash} />
<Route exact path="*" component={Home} />
</Switch>
</Router>
);
}
}
类应用程序扩展组件{
render(){
返回(
);
}
}