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

kontrolo

v0.2.0

Published

Authorisation and route manager helper

Downloads

8

Readme

kontrolo Build Status

Authorization and route manager helper

Kontrolo was craft to be used with flux, but it's a general wrapper of routes and authorizations used in the context of the timetrack's project.

Timetrack uses react-router and has to protect routes and actions depending on user rights. Kontrolo try to offer a simple way of defining authorizations on actions and routes.

Let's practice, we have to manage persons, edit, delete and list them. edit and list are two Kontrolo routes (they will be mapped to react-router routes) whereas delete is a Kontrolo action.

To edit logged user needs an admin role to delete it needs an admin role aged more than 18, and to list persons we just need an authenticate user.

Kontrolo needs a flux store that can anwer to getUser(), isLoggedIn() and getUserRoles()

let's simulate our store:

const store = {data: 'coucou'} // may be a redux store
const user = { name: 'toto', age: 13, roles: ['admin']};
const loginSelector = () => {
  return {
    getUser(){ return user },
    isLoggedIn(){ return !!this.getUser() },
    getUserRoles(){ return user.roles },
  }
}

let's define authorizations:

import {Route, RouteManager, Auth, AuthManager} from 'kontrolo';

const personAuthManager = AuthManager([
  Auth({
    name: 'delete',
    roles: ['admin'],
    method: (user, getState, context) => return user.age > 18 && getState().data === 'coucou',
  }),
], {name: 'person'});

let's define routes:

const personRoutes = RouteManager([
  Route({
    name: 'list',
    path: '/people',
    topic:'people',
    label: 'People', 
    component: PersonListApp,
    isMenu: true,
    iconName: 'users',
    authRequired: true
  }),

  Route({
    name: 'edit',
    path: '/person/edit',
    topic:'people',
    label: 'People', 
    component: PersonEditApp,
    isMenu: true,
    iconName: 'users',
    authRoles: ['admin'],
  })
], {name: 'person'});


const routes = RouteManager([
  personRoutes,
  Route({
    name: 'notfound',
    path: '/notfound',
    component: 'NotFound',
  }),
]);

Put together routes and auths:

const auths = AuthManager([
  personAuthManager,
], {
  loginSelector,
  routes,
  getState: () => store
});

Now inside our react components, we can check if an action or a route is authorized:

import auths from './auths';

if(auths.person.isAuthorized('edit')) ....
//or 
if(auths.isAuthorized('person.edit')) ....

We can see Kontrolo in action within timetrack:

  • within React components, like code above
  • in onEnter callbacks of react-router with a code like this:
function onEnter(route, nextState, replaceState){
  if(route.isAuthRequired() && !loginStore.isLoggedIn()) return replaceState({nextRouteName: this.name}, auths.login.path);   if(!auths.isAuthorized(route)) return replaceState(null, auths.unauthorized.path); 
}
  • in the react boot file, to transform Kontrolo routes in react-router routes

Install

  $ npm install
  $ npm run build
  $ npm test