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

@brdu/react-gatekeeper

v0.1.0

Published

Simplified solution for role-based access control and conditional component rendering on React applications.

Downloads

2

Readme

React Gatekeeper

A simplified solution for role-based access control and conditional component rendering on React applications.

The Gatekeeper manages access to views and conditional component rendering comparing claims found in users' JWT access tokens against a set of authorized (compound) roles. Claims hold the role(s) of each user within the application. Compound roles combine groups that users are associated with (orgTypes) and their role within each group (role).

To clarify the concept, consider John who's a sales manager at a retail company. His claims might include a global scope within the company, as well as an scope specific to the sales department. His decoded JWT token would look something like this:

{
  "sub": "1234567890",
  "name": "John Doe",
  "iat": 1516239022
  "departments": [{ // 'organizations' is John's root claims property
    "department": "global", // 'department' is the orgType property on each claim
    "role": "employee" // 'role' is orgRole property on each claim
  }, {
    "department": "sales",
    "role": "manager"
  }]
}

Therefore, John's compound roles within the company could be summarized as ['global:employee', 'sales:manager'].

Sample Quick Start

The easiest way to start using React Gatekeeper right away is learning from an example.

# Dummy application structure
|
|- <Components>
  |- IndividualPerformance.js
  |- TeamPerformance.js
|- <Views>
  |- Intranet.js
  |- SalesTeam.js
  |- SignIn.js
  |- 404.js
|- App.js
|- Gatekeeper.js

Step 1: Initialize your Gatekeeper

// ./Gatekeeper.js
import Gatekeeper from 'react-gatekeeper'

const gatekeeper = new Gatekeeper({
  rootClaimsProperty: 'departments',
  claimOrgTypeProp: 'department',
  claimOrgRoleProp: 'role',
})

gatekeeper.addRule({
  route: '/intranet',
  whitelist: ['global:employee'],
})
/* a service provider holding a claim 'global:ext' wouldn't have access to the company's intranet */
gatekeeper.addRule({
  route: '/sales-team',
  whitelist: ['sales:*'],
})

export default gatekeeper

Step 2: Layout your application routes with the Gatekeeper

// ./App.js
import React from 'react'
import {
  BrowserRouter, Switch, Route,
} from 'react-router-dom'

import gatekeeper from './Gatekeeper'
import Intranet from './Views/Intranet'
import SalesTeam from './Views/SalesTeam'
import SignIn from './Views/SignIn'
import NotFound from './Views/404'

function App() {
  const accessToken = undefined /* retrieve the user accessToken */
  gatekeeper.setUserToken(accessToken)

  const restrictedRoutes = [{
    path: '/intranet',
    component: Intranet,
  }, {
    path: '/sales-team',
    component: SalesTeam,
  }]

  return (
    <BrowserRouter>
        <Switch>
          {restrictedRoutes.map((route) => (
            gatekeeper.routeGate(route.path)
              ? <Route path={route.path} component={route.component} />
              : <Route path={route.path} component={!accessToken ? SignIn : NotFound} />
          ))}
          <Route path='/sign-in*' component={SignIn} />
          <Route path='*' component={NotFound} />
        </Switch>
    </BrowserRouter>
  )
}

export default App

Step 3: Add conditional rendering to your view components

// ./Views/SalesTeam.js
import React from 'react'

import gatekeeper from '../Gatekeeper'
import IndividualPerformance from '../Components/IndividualPerformance'
import TeamPerformance from '../Components/TeamPerformance'

function SalesTeam() {
  // TeamPerformance will be rendered only to sales managers
  // IndividualPerformance will be rendered only to sales reps
  return(<div>
    {gatekeeper.componentGate(['sales:manager'], TeamPerformance)}
    {gatekeeper.componentGate(['sales:rep'], IndividualPerformance)}
  </div>)
}

export default SalesTeam