import React from 'react'; import { useAuthContext } from '../context'; import { PrivateControlProps, RequireAuthProps, User } from "useAuth/types"; export function RequireAuth(props: RequireAuthProps) { const auth = useAuthContext(); if (!auth.user) { return <>; } if (props.permissions !== undefined && props.permissions !== null) { const userpermission = auth.user.roles; const arr: string[] = userpermission.map((object) => object); const newArray: string[] = []; for (let i = 0; i < props.permissions.length; i++) { const p = props.permissions[i]; for (let ii = 0; ii < auth.localRoles.Roles.length; ii++) { if (auth.localRoles.Roles[ii].Key === p) newArray.push(auth.localRoles.Roles[ii].RoleKey); } } const isAllowed = newArray.some((allowed: string) => arr.includes(allowed)); // Specify the type of 'allowed' as 'string' return isAllowed ? <>{props.children} :
You donot have permission to view this page
; } return <>{props.children}; } export function PrivateControl(props: PrivateControlProps) { const auth = useAuthContext(); if (!auth.user) { return <>; } if (props.permissions !== undefined && props.permissions !== null) { const userpermission = auth.user.roles; const arr: string[] = userpermission.map((object) => object); const newArray: string[] = []; for (let i = 0; i < props.permissions.length; i++) { const p = props.permissions[i]; for (let ii = 0; ii < auth.localRoles.Roles.length; ii++) { if (auth.localRoles.Roles[ii].Key === p) newArray.push(auth.localRoles.Roles[ii].RoleKey); } } const isAllowed = newArray.some((allowed: string) => arr.includes(allowed)); return isAllowed ? <>{props.children} : <>; } return <>{props.children}; } export function authHeader(): Record | undefined { const userString = sessionStorage.getItem("user"); if (userString) { const user: User = JSON.parse(userString); if (user.access_token) { return { "Content-Type": "application/json", Authorization: `Bearer ${user.access_token}`, }; } } return undefined; }