npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-auth-router-config

v1.0.1

Published

react-auth-router-config is an extension for [react-router-config](https://www.npmjs.com/package/react-router-config) which helps you authenticating access to specific routes. - generate routes based on your own auth policy - customize your own no-permi

Downloads

6

Readme

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;

NPM npm

Installation

npm install react-auth-config-router

Examples

basic demo

Usage

import React from 'react'
import { render } from 'react-dom';
import { BrowserRouter, Link } from "react-router-dom";
import { 
  authMatchRoutes, 
  authCallbackMatchRoutes,
  authRenderRoutes, 
  authCallbackRenderRoutes } from 'react-auth-config-router';

const routes = [
    {
        component: Root,
        routes: [
            {
                path: "/",
                exact: true,
                component: Home
            },
            {
                path: "/child/:id",
                component: Child,
                routes: [
                    {
                        path: "/child/:id/grand-child-render",
                        exact: true,
                        render: () => <div>Grand Child Render</div>
                    }
                ]
            }
        ]
    }
];

render(
    <BrowserRouter>
        {authRenderRoutes(routes, true)}
    </BrowserRouter>,
    document.getElementById('root'));

API

1、authRenderRoutes

authRenderRoutes(routes, hasPermission, forbiddenPage, extraProps, switchProps)

Parameters

  • routes
    the route configuration
    Note:
    auth router config provides a route attribute noAuth, which helps you ignore hasPermission authenticating.
    routes config demo as below, and path: "/test/:id" will be out of hasPermission 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 attribute permissions to authenticating, and permissions will be passed to the callback function authCallback.
    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
                    }
                ]
            }
        ]
    }
];

function authCallback(permissions) {
    return permissions.includes('master');
}

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 param hasPermission of authRenderRoutes.
    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 param hasPermission of authCallbackRenderRoutes.
    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;