Giter Club home page Giter Club logo

router-guards's Introduction

🚥 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.