Created
May 17, 2021 02:21
-
-
Save iskenxan/0ac1295d182a0eb9b2d335d657ed7b24 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { cloneElement } from "react"; | |
import { PERMISSIONS } from "./permission-maps"; | |
import { useGetRole } from "./useGetRole"; | |
const hasPermission = ({ permissions, scopes }) => { | |
const scopesMap = {}; | |
scopes.forEach((scope) => { | |
scopesMap[scope] = true; | |
}); | |
return permissions.some((permission) => scopesMap[permission]); | |
}; | |
export default function PermissionsGate({ | |
children, | |
scopes = [] | |
}) { | |
const { role } = useGetRole(); | |
const permissions = PERMISSIONS[role]; | |
const permissionGranted = hasPermission({ permissions, scopes }); | |
if (!permissionGranted) return <></> | |
return <>{children}</>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment