toastyboost / router-guards

🚥 Guards Role Based Access Control (RBAC) for React Router

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

🚥 Guards RBAC for React Router

Package provides methods that allowing you to perform complex logic (Role Based Access Control) between the call for navigation and the final render of a routes.

Storybook

Install

npm i router-guards
yarn add router-guards

Usage

~ guards.js

import { Guard } from 'router-guards';

type SessionProps = {
  role: 'admin' | null;
};

export function onlyAnon(): Guard<SessionProps> {
  return (route, context) => (context && context.role ? null : route);
}

export function onlyUsers(): Guard<SessionProps> {
  return (route, context) => (context && context.role ? route : null);
}

~ routes.js

import { onlyAnon, onlyFor } from './guards';

export const ROUTES = [
  {
    path: '/',
    component: LoginPage,
    guards: [onlyAnon()],
  },
  {
    path: '/',
    component: AdminPage,
    guards: [onlyUsers(), onlyFor(['admin'])],
  },
];

~ app.js

import { renderRoutes } from 'router-guards';
import { ROUTES } from './routes';

const Routing = () => {
  const session = { name: 'Alex', role: 'admin' };
  const currentRoutes = renderRoutes(ROUTES, session);

  return currentRoutes;
}

Features

  • Guards (RBAC)
  • Render configs (Array, Object)
  • Nested configs
  • Replace react-router-config

Attantion

В роутинге важен порядок роуетов. Если вы используете цифры в виде названия ключей, цифровые роуты всплывут в верх объекта.

TODO

  • Check no guards case
  • If name exist then key in object routes
  • Add tests
  • Add publishing features
  • Add gif example
  • Default path
  • Add app design
  • Inherit parent guards

About

🚥 Guards Role Based Access Control (RBAC) for React Router


Languages

Language:TypeScript 78.4%Language:JavaScript 21.6%