我使用 PassportJS 和 ExpressJS。我有一条通往 logout
的路线:
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
但是,当使用浏览器的后退按钮时,仍然可以显示仅供登录用户使用的页面。如何避免上述情况?谢谢
最佳答案
我认为有多种方法可以做到。我在我的应用程序中使用 Passport 的方式是,我有一个 redux reducer+action 调用“api/current_user”,然后返回一个值为 true 或 false 的 prop。如果用户已登录,则为真,如果已注销,则为假。
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>;
}
}
聊天组件/chat 会在显示任何内容之前通过 renderContent() 逻辑。任何时候你想要一个只对登录用户可见的父组件(通过 react-router-dom 负责页面的组件),只需使用 mapStateToProps -> this.props.auth。
我认为由于我们通过我们的服务器使用 oauth,因此这里使用 anchor 标记是合适的,因为流程基本上是 react -> server (当我们注销时)-> back to your react login page。因此,我认为使用是不合适的。抱歉,答案晚了几年,哈哈
关于ExpressJS 和 PassportJS 注销 - 后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18846859/