Following this example you can make a component PrivateRoute to wrap Route and use that whenever you need a route that requires auth.
This is the component code from the example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | const PrivateRoute = ({ component: Component, ...rest }) => ( <Route {...rest} render={props => ( fakeAuth.isAuthenticated ? (<Component {...props}/>) : ( <Redirect to={{ pathname: '/login', state: { from: props.location } }} /> ) )} /> ) |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.