react-authorizer

1.0.2 • Public • Published

💂‍♂️ 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

Readme

Keywords

none

Package Sidebar

Install

npm i react-authorizer

Weekly Downloads

4

Version

1.0.2

License

MIT

Unpacked Size

201 kB

Total Files

7

Last publish

Collaborators

  • sir-marc