Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

How to redirect from route if sth. (e.g. not logged in)

function createRoutes() {
  function requireLogin(store) {
    return (nextState, replaceState) => {
  
      const { users: { user }} = store.getState();
      if (_.isEmpty(user)) { // or any other authentication condition
        replaceState({ nextPathname: nextState.location.pathname }, '/');
      }
    }
  }
  
  return (
    <ReduxRouter>
      <Route path="/" component={LoginPage}/>
      <Route path="/login" component={LoginPage}/>
      <Route path="/orders" onEnter={requireLogin(store)} component={Main}>
        <Route path="/orders(/:filter)" component={HomePage}/>
        <Route path="/order/:id/:action" component={OrderPage}/>
        <Route path="/order/:id" component={OrderPage}/>
      </Route>
    </ReduxRouter>
  );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment