5

我正在使用 PassportJS 和 ExpressJS。我有一条路线logout

app.get('/logout', function(req, res){
    req.logout();
    res.redirect('/');
});

但是,当使用浏览器的后退按钮时,仍然可以显示仅供登录用户使用的页面。如何避免上述情况?谢谢

4

1 回答 1

0

我认为有多种方法可以做到这一点。我在我的应用程序中使用护照的方式是,我有一个调用“api/current_user”的redux reducer+action,然后返回一个值为真或假的道具。如果用户已登录,则为 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>;
    }
}

聊天组件,即 /chat,在显示任何内容之前会经过 renderContent() 逻辑。任何时候你想要一个只对登录用户可见的父组件(通过 react-router-dom 负责页面的组件),只需使用 mapStateToProps -> this.props.auth。

我认为由于我们通过服务器使用 oauth,因此此处适合使用锚标记,因为流程基本上是 react -> 服务器(当我们注销时)-> 返回您的 react 登录页面。因此,我认为使用是不合适的。抱歉,答案晚了几年,哈哈

于 2018-05-25T02:41:11.890 回答