From 35f4c584b081f8b42c4f4c70af59d8f7c49107c1 Mon Sep 17 00:00:00 2001 From: Ashraf Mohammed Date: Tue, 16 Jan 2024 18:03:41 +0530 Subject: [PATCH] add authorize route component --- src/Routers/routes/FacilityLocationRoutes.tsx | 5 ++++- src/Utils/AuthorizeFor.tsx | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Routers/routes/FacilityLocationRoutes.tsx b/src/Routers/routes/FacilityLocationRoutes.tsx index aa08661da8b..81896b48c98 100644 --- a/src/Routers/routes/FacilityLocationRoutes.tsx +++ b/src/Routers/routes/FacilityLocationRoutes.tsx @@ -4,6 +4,7 @@ import { AddLocationForm } from "../../Components/Facility/AddLocationForm"; import { BedManagement } from "../../Components/Facility/BedManagement"; import LocationManagement from "../../Components/Facility/LocationManagement"; import CentralLiveMonitoring from "../../Components/CameraFeed/CentralLiveMonitoring"; +import { AuthorizeUserRoute } from "../../Utils/AuthorizeFor"; export default { "/facility/:facilityId/location": ({ facilityId }: any) => ( @@ -37,6 +38,8 @@ export default { ), "/facility/:facilityId/live-monitoring": (props: any) => ( - + + + ), }; diff --git a/src/Utils/AuthorizeFor.tsx b/src/Utils/AuthorizeFor.tsx index 0ba40755205..38ca5fb4165 100644 --- a/src/Utils/AuthorizeFor.tsx +++ b/src/Utils/AuthorizeFor.tsx @@ -1,6 +1,15 @@ import { UserRole } from "../Common/constants"; +import React from "react"; +import useAuthUser from "../Common/hooks/useAuthUser"; +import Error404 from "../Components/ErrorPages/404"; export type AuthorizedForCB = (userType: UserRole) => boolean; + +interface AuthorizeUserRouteProps { + userTypes: UserRole[]; + children: React.ReactNode; +} + export type AuthorizedElementProps = { /** * Restrict access of this button to specific roles. @@ -26,3 +35,16 @@ export const Anyone = () => true; export default function (userTypes: UserRole[]) { return (userType: UserRole) => userTypes.includes(userType); } + +export const AuthorizeUserRoute: React.FC = ({ + userTypes, + children, +}) => { + const userData = useAuthUser(); + + if (userData && userTypes.includes(userData.user_type)) { + return <>{children}; + } else { + return ; + } +};