Created
June 15, 2021 21:45
-
-
Save nikasepiskveradze/2ae4c4d1664255e004e39c4c9c4ffad9 to your computer and use it in GitHub Desktop.
PrivateRoute Demo
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
export const PrivateRoute = ({ component: Component, roles, ...rest }) => ( | |
<Route {...rest} render={props => { | |
const currentUser = authenticationService.currentUserValue; | |
if (!currentUser) { | |
// not logged in so redirect to login page with the return url | |
return <Redirect to={{ pathname: '/login', state: { from: props.location } }} /> | |
} | |
// check if route is restricted by role | |
if (roles && roles.indexOf(currentUser.role) === -1) { | |
// role not authorised so redirect to home page | |
return <Redirect to={{ pathname: '/'}} /> | |
} | |
// authorised so return component | |
return <Component {...props} /> | |
}} /> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment