role-auth
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

role-auth

Simple middleware for routes to check roles and allow only request with valid roles to go forward.

Installation

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js.

If this is a brand new project, make sure to create a package.json first with the npm init command.

Installation is done using the npm install command:

$ npm install role-auth

Usage

const validateRole = require('role-auth'); // In Javascript
// OR
import validateRole from 'role-auth'; // In Typescript

Adding role check at application level

const express = require('express');
const validateRole = require('role-auth');
const app = express();


const rolesAllowed = ['admin', 'developer'];
app.use(validateRole(rolesAllowed)); // Allows only with request which has role in request headers.

Adding role check at the route level

const rolesForRoot = ['Admin', 'Developer'];
app.get('/', validateRole(rolesAllowed), (req, res) => {
    // Logic after the user with Admin/Developer role in the request header
});

const rolesForTestRoute = ['Tester'];
app.get('/testing', validateRole(rolesForTestRoute), (req, res) => {
    // Logic after the user with Tester role in the request header
});

Package Sidebar

Install

npm i role-auth

Weekly Downloads

2

Version

1.1.0

License

MIT

Unpacked Size

3.97 kB

Total Files

4

Last publish

Collaborators

  • surajumloti