forked from ddrilling/asb_cloud_front
22 lines
554 B
TypeScript
22 lines
554 B
TypeScript
|
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 } }} />)
|
||
|
}
|
||
|
/>
|
||
|
)
|
||
|
}
|