0.0.7 • Published 4 years ago

router-guards v0.0.7

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

🚥 Router guards for RBAC realization in react-router

Package provides methods that allowing you to perform complex logic between the call for navigation and the final render of a routes.

Storybook

Install

npm i router-guards yarn add router-guards

Usage

~ 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;
}
~ routes.js

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

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

import { Guard } from 'router-guards';

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

// if role dosen't exist
export function onlyAnon(): Guard<SessionProps> {
  return (route, context) => (context && context.role ? null : route);
}

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

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
0.0.7

4 years ago

0.0.6

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago