Skip to content

Instantly share code, notes, and snippets.

@jmarceli
Created October 20, 2015 08:54
Show Gist options
  • Save jmarceli/3e40d1f5cb4d4a6bb6ca to your computer and use it in GitHub Desktop.
Save jmarceli/3e40d1f5cb4d4a6bb6ca to your computer and use it in GitHub Desktop.

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