-
-
Save EfeAgare/eb7d43dde23984c9a6f5e5640314779d to your computer and use it in GitHub Desktop.
Private routes with React Router v4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function PrivateRoute ({component: Component, authenticated, ...rest}) { | |
return ( | |
<Route | |
{...rest} | |
render={(props) => authenticated === true | |
? <Component {...props} /> | |
: <Redirect to={{pathname: '/login', state: {from: props.location}}} />} | |
/> | |
) | |
} | |
<Route path='/' exact component={Home} /> | |
<Route path='/login' component={Login} /> | |
<Route path='/register' component={Register} /> | |
<PrivateRoute authenticated={this.state.authenticated} path='/invoices' component={Invoices} /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment