1.1.0 • Published 4 years ago
role-auth v1.1.0
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
});