Socket
Socket
Sign inDemoInstall

@mknet/react-acl-router

Package Overview
Dependencies
1
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @mknet/react-acl-router

Router with Access Control for React Applications.


Version published
Maintainers
1
Install size
329 kB
Created

Readme

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

Last updated on 03 Sep 2018

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