Skip to content

Connect/Express middleware to validate if a user belongs to a group

License

Notifications You must be signed in to change notification settings

chefsplate/connect-crew

Repository files navigation

connect-crew Build Status

Connect/Express middleware to validate if a user belongs to a group. Works nicely with passport.

Install

$ npm install --save connect-crew

Requirements

Usage

By default, connect-crew assumes your authentication middleware returns req.user.groups as an Array. Check path option if you need to change it.

Single request

const express = require('express';)
const crew = require('connect-crew');
const passport = require('passport');

app.use(passport.authenticate('local'));

app.get('/maria-hill',
	crew('staff'),
	(req, res, next) => res.json({ result: true })
);

app.get('/black-widow',
	crew(['avenger', 'staff']),
	(req, res, next) => res.json({ result: true })
);

All requests

const express = require('express';)
const crew = require('connect-crew');
const passport = require('passport');

app.use(passport.authenticate('local'))
app.use(crew('staff'));

API

crew(input, [options])

input

Type: string or array

Groups to validate.

options

path

Type: string
Default: user.groups

Groups array path inside req object.

error

Type: object
Default: { code: 401, message: 'Unauthorized'}

Error message when user doesn't belong to a group.

Set default options

crew.options({
	path: 'member.crew',
	error: {
		code: 401,
		message: 'You are not welcome'
	}
});

Develop

Inside the project folder:

npm i
npm start

License

MIT © Chef's Plate

About

Connect/Express middleware to validate if a user belongs to a group

Resources

License

Stars

Watchers

Forks

Packages

No packages published