react-identity
Higher order components for composing authorization into react apps
Features
- Integrates with your existing components seamlessly
- Works with redux and other stores
Installation
Using npm:
npm install react-identity
Importing:
// Using ES6 transpiler
import ReactIdentity from 'react-identity'
// Or else
const ReactIdentity = require('react-identity')
Usage
react-identity
provides withAuthorization
HOC for composing authorization
Example component:
{ return <div>thispropsusername</div> }
Lets see how we can add authorization to this component
withAuthorization HOC
withAuthorization
takes a config which in this case is [ 'admin', 'moderator' ] which is later used by the authorize
function to check if the component can be rendered.
{ return <div>Sample Component</div> } 'admin' 'moderator'SampleComponent
In order for this to work, we need to configure AuthProvider
with the authorize
function
AuthProvider
AuthProvider
maintains the authData
state and authorize
function in its state and is passed as childContext, and the same is utilized by the withAuthorization
HOC.
authorize
function receives the requirements passed by the withAuthorization
and userData
from the state. The implementation of authorize function is left for the user.
let { let userRole = authDatauserrole return requirements}
We have now setup the AuthProvider
and withAuthorization
but the userData
is not yet present in the AuthProvider
. authData
can be directly sent as a prop to the AuthProvider
, but for most cases this won't suffice, as authData
needs to be dynamic and can be updated at any point of time. So, updater
function can be used to update the authData
at any point of time. This can be provided as a prop to the AuthProvider
or can be accessed within the components using withUpdater
HOC.
Implementing the updater function:
let { let userRole = authDatauserrole return requirements} let { let authData = name: 'Sample User' role: 'admin' }
withUpdater HOC
{ let authData = user: name: 'Sid' role: 'moderator' thisprops } { return <div> Sample Component <input type="button" value="Change user to Sid" onClick=thishandleOnClick /> </div> } SampleComponent
Configuring updater for redux
If the authData
is stored in the redux, updates to the store can be subscribed by passing the updater
function to the AuthProvider
as follows
const store = let { // set the authData initially // subscribe to updates on the store to update the authData store}
Rendering custom Element for unauthorized users
If you want to render a custom element for unauthorized users, you can pass a react element to the withAuthorization
's config as follows
{ return <div>SampleComponent</div> }
Rendering custom Component for unauthorized users
If you want to render a custom component for unauthorized users, you can pass a component to the withAuthorization
's config as follows
{ return <div>SampleComponent</div> } { return <div>SampleUnauthorizedComponent</div> } 'admin' 'moderator' UnauthorizedComponent: SampleUnauthorizedComponent SampleComponent
Accessing authData from components
The authData
which is passed to the AuthProvider
can be accessed via react context. We also provide a decorator withAuthData
{ return <div>thispropsauthDatausername</div> } SampleComponent
Rendering components only when unauthorized
When a component needs to be rendered only when unauthorized, inverseAuth
config can be sent to the withAuthorization
{ return <div>SampleComponent</div> } inverseAuth: true
License
react-identity
is released under the MIT license.