简体   繁体   English

Redux React 路由器:无法访问受保护/私有路由

[英]Redux React Router: Cannot Access Protected/Private Route

I have a protected route that can only be accessed if the user has logged in and then isAuthenticated set to true.我有一个受保护的路由,只有在用户登录然后将 isAuthenticated 设置为 true 时才能访问该路由。 The issue I am having is, even after successfully logging in and being authenticated, I am getting redirected to login page.我遇到的问题是,即使在成功登录并通过身份验证后,我也会被重定向到登录页面。 In my redux reducer file below, the isAuthenticated state is set to null by default.在我下面的 redux reducer 文件中,isAuthenticated 状态默认设置为 null。 The weird part is that if I set isAuthenticated default value to true and then login successfully, I can access the protected route.奇怪的是,如果我将 isAuthenticated 默认值设置为 true 然后登录成功,我可以访问受保护的路由。 However, when isAuthenticated is set to null, I am unable to access the protected route even if I successfully login.但是,当 isAuthenticated 设置为 null 时,即使我成功登录,我也无法访问受保护的路由。 Would greatly appreciate it if someone can help.如果有人可以提供帮助,将不胜感激。 Thank you.谢谢你。

//Redux Reducer File //Redux Reducer 文件

import{
  LOGIN_FAIL,
  LOGIN_SUCCESS,
  USER_LOADING,
  USER_LOADED,
  AUTH_ERROR
} from '../actions/types';

const initialState = {
  token: localStorage.getItem('token'),
  isAuthenticated: null,
  isLoading: false,
  user: null
};

export default function(state = initialState, action){
  switch(action.type){
    case USER_LOADING:
      return{
        ...state,
        isLoading: true
      };
    case USER_LOADED:
      return{
        ...state,
        isAuthenticated: true,
        isLoading: false,
        user: action.payload
      };
    case LOGIN_SUCCESS:
    localStorage.setItem('token', action.payload.token);
    return{
      ...state,
      ...action.payload,
      isAuthenticated: true,
      isLoading: false
    };
    case LOGIN_FAIL:
    case AUTH_ERROR:
    localStorage.removeItem('token');
      return{
        ...state,
        token: null,
        isAuthenticated: false,
        isLoading: false,
        user: null
      };

    default:
      return state;
  }

}

//ProtectedRoute file //受保护的路由文件

export const ProtectedRoute = ({
  isAuthenticated,
  component: Component,
  ...rest
})=>(
  <Route {...rest} component={(props) =>(
    isAuthenticated ? (
      <Component {...props}/>
    ):(
      <Redirect to="/login"/>
    )
  )}/>

)

ProtectedRoute.propTypes = {
  isAuthenticated: PropTypes.bool
}
const mapStateToProps = state =>({
  isAuthenticated: state.auth.isAuthenticated
});

export default connect(mapStateToProps)(ProtectedRoute)

//App.js File //App.js文件

class App extends Component{
  componentDidMount(){
    store.dispatch(loadUser());
  }
  render(){
  return(
  <Provider store={store}>
  <div className="App">
    <Router>
      <Switch>
        <Route exact path="/login" component={LoginPage}/>
        <ProtectedRoute exact path="/userfeed" component={UserFeed}/>
      </Switch>
    </Router>
  </div>
  </Provider>

);
}
};

export default App;

Change like this in your app.js在你的 app.js 中像这样改变

   <Switch>
 { !isAuthenticated ?  <Route exact path="/login" component={LoginPage}/>
   : <ProtectedRoute exact path="/userfeed" component={UserFeed}/> }
  </Switch>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM