Redirect to login page in react router 4
I usually create a PrivateRoute
component that checks if the user is logged in (via prop, redux, localstorage or something).
Something like:
const PrivateRoute = ({ isLoggedIn, ...props }) =>
isLoggedIn
? <Route { ...props } />
: <Redirect to="/login" />
In the router I then use it for my, well, private routes :)
<Switch>
<PrivateRoute isLoggedIn={ this.state.loggedIn } path="/protected" component={Protected} />
<Route path="/login" component={Login}/>
</Switch>