asb_cloud_front/src/components/Private/PrivateRoute.tsx

22 lines
554 B
TypeScript
Raw Normal View History

import React from 'react'
import { Route, Redirect } from 'react-router-dom'
import { isInRole, Role } from './PrivateContent'
type PrivateRouteProps = {
children: any
roles: Role[]
[other: string]: any
}
export const PrivateRoute: React.FC<PrivateRouteProps> = ({ children, roles, ...other }) => {
const token = localStorage['token']
return (
<Route
{...other}
render={({ location }) => token && isInRole(roles) ? children : (
<Redirect to={{ pathname: "/login", state: { from: location } }} />)
}
/>
)
}