rmn316 / NIMBLE

Nimble developer assignment
0 stars 0 forks source link

#29 Added redirect to default route to send a user to the login page … #30

Open rmn316 opened 5 years ago

rmn316 commented 5 years ago

What Happened

Application does not automatically redirect a non-authenticated user to the login page.

Outcome

Added a redirect to the login page on the default route page (component). Which will send any non-authenticated user to the login page.

olivierobert commented 5 years ago

A more standard approach with React Router >= 4.x.x . is to create a component which returns a <Route> or <Redirect>:

const AuthRequiredRoute = ({ component: Component, ...rest }) => (
  <Route {...rest} render={(props) => (
     isAuthenticated === true
      ? <Component {...props} />
      : <Redirect to={{
          pathname: '/login',
          state: { from: props.location }
        }} />
  )} />
)

Then:

<Switch>
  <Route path="/logout" component={Logout} />
  <Route path="/login" component={Login} />
  <AuthRequiredRoute path="/" exact component={KeywordItems} />
</Switch>