react-auth-router-config
react-auth-router-config is an extension for react-router-config which helps you authenticating access to specific routes.
- generate routes based on your own auth policy
- customize your own no-permission component/render
- matchRoutes depends on your own auth policy;
Installation
npm install react-auth-config-router
Examples
Usage
;;; const routes = component: Root routes: path: "/" exact: true component: Home path: "/child/:id" component: Child routes: path: "/child/:id/grand-child-render" exact: true <div>Grand Child Render</div> ; ;
API
1、authRenderRoutes
authRenderRoutes(routes, hasPermission, forbiddenPage, extraProps, switchProps)
Parameters
- routes
the route configuration
Note:
auth router config provides a route attributenoAuth
, which helps you ignorehasPermission
authenticating.
routes config demo as below, andpath: "/test/:id"
will be out ofhasPermission
authenticating(it means the path has permission).
const routes = component: Root routes: path: "/" exact: true permissions: component: Home path: "/test/:id" component: Child noAuth: true routes: path: "/test/:id/test-child" component: GrandChild path: "/child/:id" component: Child routes: path: "/child/:id/grand-child" component: GrandChild ;
- hasPermission
this is a global param to control whether a route component/render should be presented. - forbiddenPage
if a route has no permission, the forbiddenPage would be presented.
forbiddenPage
could be functional component or class component.
Default:
const ForbiddenPage = <div> <h3>403 Forbidden!</h3> </div>;
- extraProps
Default:{}
- switchProps
Default:{}
2、authCallbackRenderRoutes
authCallbackRenderRoutes(routes, authCallback, forbiddenPage, extraProps, switchProps)
Parameters
all params are same as authRenderRoutes
except authCallback
.
- authCallback
we provide a route attributepermissions
to authenticating, andpermissions
will be passed to the callback functionauthCallback
.
Note:
authCallback
must be synchronous;
if you do not pass any callback function and it means all routes has permission;
const routes = component: Root routes: path: "/" exact: true permissions: component: Home path: "/test/:id" component: Child permissions: 'test' routes: path: "/test/:id/test-child" component: GrandChild path: "/child/:id" component: Child permissions: 'master' routes: path: "/child/:id/grand-child" component: GrandChild ; { return permissions;}
3、authMatchRoutes
authMatchRoutes(hasPermission, routes, pathname, branch)
Parameters
all params are same as matchRoutes of react-router-config except hasPermission
;
- hasPermission
it is same as the paramhasPermission
ofauthRenderRoutes
.
you could go to basic demo for details.
3、authCallbackMatchRoutes
authCallbackMatchRoutes(authCallback, routes, pathname, branch)
Parameters
all params are same as matchRoutes of react-router-config except authCallback
;
- authCallback
it is same as the paramhasPermission
ofauthCallbackRenderRoutes
.
you could go to basic demo for details.
Note:
authCallback
must be synchronous;
if you do not pass any callback function and it means all routes has permission;