Skip to content

Instantly share code, notes, and snippets.

@juliomerisio
Forked from sibelius/motivation.md
Created April 25, 2020 03:07
Show Gist options
  • Save juliomerisio/bb864c0c76e416e6c40d4998868eeee0 to your computer and use it in GitHub Desktop.
Save juliomerisio/bb864c0c76e416e6c40d4998868eeee0 to your computer and use it in GitHub Desktop.
usePermission react hook to handle frontend authorization concerns

why is this a React hook?

this is a simplified version of the production/real world version

You can consume roles from context/redux/relay or other place to improve dx and void bugs

const App = ({ user }) => {
const { hasRole } = usePermission(user);
return (
<span>Hello</span>
{hasRole('PRODUCT:CREATE') && <button>Create Product</button>
);
}
const usePermission = (user: UserPermission_user) => {
const hasRole = (role: string) => {
return user.roles.includes(role);
};
return {
hasRole
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment