Skip to content

Commit

Permalink
add authorize route component
Browse files Browse the repository at this point in the history
  • Loading branch information
AshrafMd-1 committed Jan 16, 2024
1 parent bceefa5 commit 35f4c58
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Routers/routes/FacilityLocationRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (
Expand Down Expand Up @@ -37,6 +38,8 @@ export default {
<AddBedForm facilityId={facilityId} locationId={locationId} bedId={bedId} />
),
"/facility/:facilityId/live-monitoring": (props: any) => (
<CentralLiveMonitoring {...props} />
<AuthorizeUserRoute userTypes={["StateAdmin", "DistrictAdmin", "Doctor"]}>
<CentralLiveMonitoring {...props} />
</AuthorizeUserRoute>
),
};
22 changes: 22 additions & 0 deletions src/Utils/AuthorizeFor.tsx
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -26,3 +35,16 @@ export const Anyone = () => true;
export default function (userTypes: UserRole[]) {
return (userType: UserRole) => userTypes.includes(userType);
}

export const AuthorizeUserRoute: React.FC<AuthorizeUserRouteProps> = ({
userTypes,
children,
}) => {
const userData = useAuthUser();

if (userData && userTypes.includes(userData.user_type)) {
return <>{children}</>;
} else {
return <Error404 />;
}
};

0 comments on commit 35f4c58

Please sign in to comment.