1.0.0 • Published 4 years ago

react-acl-router v1.0.0

Weekly downloads
41
License
MIT
Repository
github
Last release
4 years ago

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.
1.0.0

4 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago