Skip to content

Instantly share code, notes, and snippets.

@iLTW1n
Forked from rcanepa/routes.js
Created August 2, 2018 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iLTW1n/83d911d95afcb252b68c0c062cf89d4a to your computer and use it in GitHub Desktop.
Save iLTW1n/83d911d95afcb252b68c0c062cf89d4a to your computer and use it in GitHub Desktop.
Private routes with React Router v4
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