Socket
Socket
Sign inDemoInstall

react-authorization-handler

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-authorization-handler

An easy to use authorization library for react


Version published
Weekly downloads
2
Maintainers
1
Install size
15.6 kB
Created
Weekly downloads
 

Readme

Source

react-authorization

This Library provides hooks and HOCs to easily handle authorizations in yout react application. Since it just dependes on reacts context and hooks its very lightweight.

Install

npm install react-auhtorization

Getting startet

Wrap your root component in AuthorizationProvider to pass the context and set the default-/initialRole. If you are using Typescript you can pass a Permission Type to the Role. To check Authorization, use the AuthorizationChecker Component.

type Permissions = "askForTest" | "doThis" | "doThat";
const defaultRole: Role<TestPermissions> = {
  name: "Guest",
  permissions: ["askForTest"]
};
function App(){
  return(
    <AuthorizationProvider initialRole={defaultRole}>
        <AuthorizationChecker<Permissions> requiredPermission="doThis">
          <div>
            <h1>This won't be shown</h1>
          </div>
        </AuthorizationChecker>
    </AuthorizationProvider>)}
type Permissions = "askForTest" | "doThis" | "doThat";
const defaultRole: Role<TestPermissions> = {
  name: "Guest",
  permissions: ["askForTest"]
};
function App(){
  return(
    <AuthorizationProvider initialRole={defaultRole}>
        <AuthorizationChecker<Permissions> requiredPermission="doThis" fallback={ <div><h1>Fallback</h1></div>}>
          <div>
            <h1>The Fallback will be shown</h1>
          </div>
        </AuthorizationChecker>
    </AuthorizationProvider>)}
type Permissions = "askForTest" | "doThis" | "doThat";
const defaultRole: Role<TestPermissions> = {
  name: "Guest",
  permissions: ["askForTest"]
};
function App(){
  return(
    <AuthorizationProvider initialRole={defaultRole}>
        <AuthorizationChecker<Permissions> requiredRole="Guest">
          <div>
            <h1>This would be shown</h1>
          </div>
        </AuthorizationChecker>
    </AuthorizationProvider>)}
type Permissions = "askForTest" | "doThis" | "doThat";
const defaultRole: Role<TestPermissions> = {
  name: "Guest",
  permissions: ["askForTest"]
};
function App(){
  return(
    <AuthorizationProvider initialRole={defaultRole}>
        <AuthorizationChecker<Permissions> allowedRoles={["Admin","User"]}>
          <div>
            <h1>This won't be shown</h1>
          </div>
        </AuthorizationChecker>
    </AuthorizationProvider>)}
function YourComponent() {
  const {authentificate,executeIfGranted,hasPermission,isAllowedRole,isRequiredRole } = useAuthority<Permissions>();
  const onLogin=()=>{
    ...
    userRole:Role<Permssions>={name:"Admin",permssions:["doThis","doThat"]}
    authentificate(userRole)
  }
  return ...
}

References

Types

Role

(P is a Permission type)

Propertytypedescription
namestringname of the role
permissionsP[]list of permissions which are granted for the role

Components

AuthorizationProvider
Propertytypedefaultdescription
initialRoleRole

{name:Guest}the default role which is used before authentificate is called
AuthorizationChecker
Propertytypedescription
allowedRolesstring[]a list of roles whoch are allowed to see the wrapped component
fallbackReacNodea react component which should be rendered instead the wrapped one if denied
requiredPermissionP extends stringthe permission which is required to see the wrapped component
requiredRolestringa role which is required to see the wrapped component

Hooks

ExecuteIfGrantedOptions{                 |
      allowedRoles?: string[];
      onDenied?: () => void;
      requiredPermission?: P;
      requiredRole?: string;
    }
useAuthorization

()

returns:

Propertytypedescription
authentificate(role:Role

):void

function to authentificate with a new role
executeIfGranted(()=>any,options:ExecuteIfGrantedOptions ):voidfunction to run functionif it is granted
hasPermission(permission:P extends string):booleanfunction to check if role has permission
isAllowedRole( allowedRoles:string[] ):booleanfunction to check if current roleis allowed role
isRequiredRole(requiredRole:string):booleanfunction to check if current role is reuired role

Keywords

FAQs

Last updated on 15 Mar 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc