|
@@ -77,6 +77,49 @@ export const HashRoute = ({component, render, ...props}) => {
|
|
|
return <Render match={match} history={history} location={location} />
|
|
|
}
|
|
|
|
|
|
+type GetUserRolesFunc = () => Array<string>
|
|
|
+
|
|
|
+export const createPrivateRoute = (getUserRoles:GetUserRolesFunc, defaultFallback:string|Function, defaultRouteRoles:string[]):Function =>
|
|
|
+ (RouteComponent:ComponentType):ComponentType =>
|
|
|
+ ({fallback, roles=[], render, component, ...props}) => {
|
|
|
+ const Render:ComponentType = component || render
|
|
|
+
|
|
|
+ const intersectArrays = (first:Array<any>, second:Array<any>) =>
|
|
|
+ first.find(firstItem => second.find(secondItem => firstItem === secondItem))
|
|
|
+
|
|
|
+ const ComponentWrapper:ComponentType = (props) => {
|
|
|
+ fallback ||= defaultFallback
|
|
|
+ roles:Array<string> ||= defaultRouteRoles
|
|
|
+
|
|
|
+ const userRoles:Array<string> = getUserRoles()
|
|
|
+
|
|
|
+ if (!fallback){
|
|
|
+ throw new ReferenceError('fallback or defaultFallback are mandatory for Private Route')
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!intersectArrays(roles, userRoles)){
|
|
|
+ if (typeof fallback === 'string')
|
|
|
+ props.history.push(fallback)
|
|
|
+ if (typeof fallback === 'function')
|
|
|
+ fallback(props.history, props)
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return <Render {...props} />
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!getUserRoles || typeof getUserRoles !== 'function'){
|
|
|
+ throw new ReferenceError('getUserRoles are mandatory for Private Routing and it should be a function')
|
|
|
+ }
|
|
|
+
|
|
|
+ return <RouteComponent {...props} component={ComponentWrapper}/>
|
|
|
+ }
|
|
|
+
|
|
|
+console.log(createPrivateRoute)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
//export const HashNamedRoute = ({name, routeName, query, pendingQueryComponent:P, errorQueryComponent:E, errorQueryComponentErrorProp="error", componentQueryResultProp='data', path, component, render, ...props}) => {
|
|
|
|
|
|
|