React Redux Cookie身份验证

时间:2019-05-30 04:29:44

标签: reactjs react-redux

用户登录时,服务器将设置HTTPS cookie。作为响应,正如您在代码中看到的那样,我每次都发出一个检查会话有效性的请求。另外,刷新后似乎即使我已登录也已注销〜1秒,因为react等待服务器响应。我不确定这是否是最好的方法,但是我看不到其他任何方法。

class App extends Component {
  componentWillMount() {
    this.props.checkLoggedIn(); // this sends a GET request to the server with the client's cookie. The server looks up the session in redux and if it's valid, the user is logged in.
  }
  render() {
    return (
      <BrowserRouter>
        <Navbar />
        <div className="App">
          <Switch>
            <Route exact path="/" component={Dashboard} />
            <Route path="/login" component={Login} />
            <ProtectedRoute path="/support" isAuthenticated={this.props.isAuthenticated} component={Tickets} />
          </Switch>
        </div>
      </BrowserRouter>
    );
  }
}
const mapStateToProps = state => {
  return {
    isAuthenticated: state.auth.isAuthenticated // true or false
  };
};
export default connect(
  mapStateToProps,
  { checkLoggedIn }
)(App);

0 个答案:

没有答案