Skip to content

Instantly share code, notes, and snippets.

@nikasepiskveradze
Created June 15, 2021 21:45
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 nikasepiskveradze/2ae4c4d1664255e004e39c4c9c4ffad9 to your computer and use it in GitHub Desktop.
Save nikasepiskveradze/2ae4c4d1664255e004e39c4c9c4ffad9 to your computer and use it in GitHub Desktop.
PrivateRoute Demo
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