JR *_*lia 5 express passport.js
我使用PassportJS和ExpressJS.我有一条路线logout:
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
Run Code Online (Sandbox Code Playgroud)
但是,当使用浏览器的后退按钮时,仍然可以显示仅供登录用户使用的页面.如何避免上述情况?谢谢
小智 0
我认为有多种方法可以做到这一点。我在应用程序中使用 Passport 的方式是,我有一个调用“api/current_user”的 redux reducer+action,然后返回一个值为 true 或 false 的 prop。如果用户已登录则为 true,如果注销则为 false。
class ChatContainer extends Component {
renderContent() {
switch (this.props.auth) {
case null:
return;
case false:
return <Redirect to="/" />;
default:
return [<ChatHeader key="1" />, <ChatWindow key="2" />];
}
}
render() {
return <div className="chat-container">{this.renderContent()}</div>;
}
}
Run Code Online (Sandbox Code Playgroud)
聊天组件(即 /chat)在显示任何内容之前都会执行 renderContent() 逻辑。任何时候你想要一个父组件(通过react-router-dom负责页面的组件)只对登录用户可见,只需使用mapStateToProps -> this.props.auth。
我认为由于我们通过服务器使用 oauth,因此锚标记在这里是合适的,因为流程基本上是反应 -> 服务器(当我们注销时) -> 返回到你的反应登录页面。因此我认为使用是不合适的。抱歉,答案晚了几年,哈哈
| 归档时间: |
|
| 查看次数: |
390 次 |
| 最近记录: |