import * as React from 'react';
import {Redirect, Route,} from 'react-router-dom';
import ReactIf from "../components/shared/ReactIf";
import useAuthService from "../hooks/useAuthService";
export function AdminGuardedRoute(props: { component: any; path: string; isAuthorized: boolean, exact: boolean }): JSX.Element {
const {component: Component, isAuthorized, ...rest} = props;
const {isUserLoggedIn} = useAuthService();
return (
<Route
{...rest}
render={(routeProps) =>
<div>
{/* If not logged in, redirect to Login page */}
<ReactIf condition={!isUserLoggedIn()}>
<Redirect
to={{
pathname: '/login',
state: {from: routeProps.location}
}}
/>
</ReactIf>
{/* If logged in and has Sys Admin role, navigate to component */}
<ReactIf condition={isAuthorized}>
<Component {...routeProps} />
</ReactIf>
{/* If logged in but does not have Sys Admin role, navigate to Unauthorized page */}
<ReactIf condition={!isAuthorized}>
<Redirect
to={{
pathname: '/unauthorized',
state: {from: routeProps.location}
}}
/>
</ReactIf>
</div>
}
/>
);
}
Created
August 13, 2021 15:41
-
-
Save pavankjadda/5b235cc608197847fa1f7587492640db to your computer and use it in GitHub Desktop.
Sys Admin Guarded Route
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment