Skip to content

Instantly share code, notes, and snippets.

@eladcandroid
Created December 23, 2018 05:52
Show Gist options
  • Save eladcandroid/9081d3caaed5c579ceb12852683308d1 to your computer and use it in GitHub Desktop.
Save eladcandroid/9081d3caaed5c579ceb12852683308d1 to your computer and use it in GitHub Desktop.
React PrivateRoute
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/hello" component={Hello} />
<Route path="/books" component={Books} />
<Route path="/login" component={Login}/>
<PrivateRoute authed={fakeAuth.isAuthenticated} path="/admin" component={Admin} />
</Switch>
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={props => (
fakeAuth.isAuthenticated ? (
<Component {...props}/>
) : (
<Redirect to={{
pathname: '/login',
state: { from: props.location }
}}/>
)
)}/>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment