Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@mknet/react-acl-router

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mknet/react-acl-router

Router with Access Control for React Applications.

  • 0.2.8
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

react-acl-router

npm v npm dm

Router with Access Control for React Applications.

Installation

yarn add react-acl-router react react-router-dom lodash

Usage

AclRouter

PropertyDescriptionTypeDefault
authoritiespermissions of current userOneOfType([string, array, func])''
authorizedRoutesarray of routes needs permissionsarrayOf(AuthorizedRoute)[]
authorizedLayoutcontainer of all authorized routesfunction<div>{props.children}</div>
normalRoutesarray of routes don't need permissionsarrayOf(NormalRoute)[]
normalLayoutcontainer of all routes don't need permissionsfunction<div>{props.children}</div>
notFoundelement to show when route doesn't matchfunction<div>404</div>

AuthorizedRoute

with all react-router <Route /> supported props except render because react-acl-router will overwrite the render prop.

PropertyDescriptionTypeDefault
pathroute's full pathstring-
permissionsarray of roles which have permission like ['god', 'admin' ]arrayOf(string)-
componentroute's componentfunction-
unauthorizedunauthorized view component if authorities don't have permissionstring-
redirectredirect path if authorities don't have permissionstring-

NormalRoute (with react-router Route's all supported props)

with all react-router <Route /> supported props except render because react-acl-router will overwrite the render prop.

PropertyDescriptionTypeDefault
pathroute's full pathstring-
redirectredirect route path to other routestring-
componentroute's componentfunction-

Example

import AclRouter from 'react-acl-router';
import BasicLayout from 'layouts/BasicLayout';
import NormalLayout from 'layouts/NormalLayout';
import Login from 'views/login';
import WorkInProgress from 'views/workInProgress';
import Unauthorized from 'views/unauthorized';

const authorizedRoutes = [{
  path: '/dashboard/analysis/realtime',
  exact: true,
  permissions: ['admin', 'user'],
  redirect: '/login',
  component: WorkInProgress,
}, {
  path: '/dashboard/analysis/offline',
  exact: true,
  permissions: ['admin', 'user'],
  redirect: '/login',
  component: WorkInProgress,
}, {
  path: '/dashboard/workplace',
  exact: true,
  permissions: ['admin', 'user'],
  redirect: '/login',
  component: WorkInProgress,
}, {
  path: '/exception/403',
  exact: true,
  permissions: ['god'],
  component: WorkInProgress,
  unauthorized: Unauthorized,
}];

const normalRoutes = [{
  path: '/',
  exact: true,
  redirect: '/dashboard/analysis/realtime',
}, {
  path: '/login',
  exact: true,
  component: Login,
}];

const Router = (props) => (
  <AclRouter
    // sync user authorities with the user data in your application
    authorities={props.user.authorities}
    authorizedRoutes={authorizedRoutes}
    authorizedLayout={BasicLayout}
    normalRoutes={unAuthorizedRoutes}
    normalLayout={NormalLayout}
    notFound={() => <div>Page Not Found</div>}
  />
);

export default Router;

Notes

  • For normal route, redirect or unauthorized and component are exclusive since normally you won't redirect user to another path while you have a valid component to render.

FAQs

Package last updated on 27 Jul 2018

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc