-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPermissionProtectedComponent.tsx
36 lines (28 loc) · 1.1 KB
/
PermissionProtectedComponent.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import React, { useEffect, useState } from "react";
import { PermissionCheck } from "@warrantdev/warrant-js";
import useWarrant from "./useWarrant";
export interface PermissionProtectedComponentProps extends PermissionCheck {
children: React.ReactNode;
}
const PermissionProtectedComponent: React.FunctionComponent<PermissionProtectedComponentProps> = ({ children, permissionId, consistentRead, debug }) => {
const [showChildren, setShowChildren] = useState<boolean>(false);
const { sessionToken, hasPermission } = useWarrant();
useEffect(() => {
if (!permissionId) {
throw new Error("Invalid or no permissionId provided to PermissionProtectedComponent");
}
const checkWarrant = async () => {
setShowChildren(await hasPermission({ permissionId, consistentRead, debug }));
}
if (sessionToken) {
checkWarrant();
}
}, [sessionToken, JSON.stringify(permissionId)]);
if (showChildren) {
return <>
{children}
</>;
}
return null;
};
export default PermissionProtectedComponent;