2021-12-02 15:10:55 +05:00
|
|
|
import React from 'react'
|
|
|
|
import { Route, Redirect } from 'react-router-dom'
|
2021-12-07 19:45:13 +05:00
|
|
|
import { Role, Permissions, hasAccess } from '../../utils/permissions'
|
2021-12-02 15:10:55 +05:00
|
|
|
|
|
|
|
type PrivateRouteProps = {
|
2021-12-07 19:45:13 +05:00
|
|
|
permissions?: Permissions
|
|
|
|
roles: Role[]
|
|
|
|
component?: any
|
|
|
|
children?: any
|
|
|
|
[other: string]: any
|
2021-12-02 15:10:55 +05:00
|
|
|
}
|
|
|
|
|
2021-12-07 19:45:13 +05:00
|
|
|
export const PrivateRoute: React.FC<PrivateRouteProps> = ({ permissions, roles, component, children, ...other }) => {
|
|
|
|
const available = localStorage['token'] && hasAccess({ permissions, roles })
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Route {...other}
|
|
|
|
component={available && component}
|
|
|
|
render={({ location }) => available ? children : (
|
|
|
|
<Redirect to={{ pathname: '/login', state: { from: location } }} />
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
)
|
2021-12-02 15:10:55 +05:00
|
|
|
}
|