Created
December 23, 2018 05:52
-
-
Save eladcandroid/9081d3caaed5c579ceb12852683308d1 to your computer and use it in GitHub Desktop.
React PrivateRoute
This file contains 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
<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> |
This file contains 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
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