登录私人路由后reactjs重定向

时间:2018-11-23 16:38:02

标签: reactjs authentication login

我建立了一个连接页面。在此连接页面之后,我希望重定向到我的仪表板。我在中间准备了具有2个值的localStorage:令牌和isAuthenticated。 但是重新路由不起作用。我总是留在同一页面上

此外,我的印象是我的登录页面启动时加载了Dashboard.jsx页面,因为我在日志中看到:auth Dashboard.jsx:33:null

我的登录页面代码:

      axios.post('/login',params)
  .then(response=> {
    localStorage.setItem(ACCESS_TOKEN, response.headers.authorization);
    localStorage.setItem(IS_AUTHENTICATED, true);

    this.setState({isAuthenticated: true });
  })

  render() {    
if(this.state.isAuthenticated){
  return <Redirect to={"/dashboard"}/>;
}else{
  return LoginTemplate.call(this);
}

“我的信息中心”页面:

const switchRoutes = (
 <Switch>
   {dashboardRoutes.map((prop, key) => {
     if (prop.redirect){
       return <Redirect from={prop.path} to={prop.to} key={key} />;
     }else if(prop.private){
       console.log("auth : " + localStorage.getItem(IS_AUTHENTICATED));
       return <PrivateRoute authenticated= 
        {localStorage.getItem(IS_AUTHENTICATED)} path={prop.path} component= 
         {prop.component} key={key} />;
     }else{
      return <Route path={prop.path} component={prop.component} key={key} 
          />;
     }
    })}
   </Switch>
  );

0 个答案:

没有答案
相关问题