react-authorizer
v1.0.2
Published
React Authorizer using the new Context API
Downloads
4
Readme
💂♂️ react-authorizer 💂♀️
Basic authorization library for React using the new Context API.
WIP
This module is no finished yet, things to come:
- Better documentation
- More examples
Usage
Wrap your React tree inside the AuthProvider Component and pass your users roles as an Array.
<AuthProvider roles={[ 'user', 'admin' ]}>
{/* your react tree */}
</AuthProvider>
And then somewhere inside this tree:
<Authorize neededRoles={[ 'user', 'admin' ]}>
{
({ isAuthorized, missingRoles, lacksRole }) => {
if (isAuthorized) {
return 'Welcome buddy';
} else if (lacksRole('admin')) {
return 'Sorry you are not an admin';
} else {
return 'You are not authorized too see anything here'
}
}
}
</Authorize>
Please reffer to the examples folder too see more :)
API
AuthProvider
- Props:
- roles: Array
Authorize
- Props:
- neededRoles: Array
- children: ({ isAuthorized: Boolean, missingRoles: Array, lacksRole: (role: String) => Boolean}) => any