Skip to content

Instantly share code, notes, and snippets.

@iskenxan
Created May 17, 2021 02:21
Show Gist options
  • Save iskenxan/0ac1295d182a0eb9b2d335d657ed7b24 to your computer and use it in GitHub Desktop.
Save iskenxan/0ac1295d182a0eb9b2d335d657ed7b24 to your computer and use it in GitHub Desktop.
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