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

express-voter

v1.0.2

Published

Add voters to your Express application

Downloads

2

Readme

Express-voter

Instal

npm i --save express-voter

How to use

Before use validation from request or from middleware, you have to implement your system user management, for example with passport.

  1. Add voters
// app.js

// Require module
const expressVoter = require('express-voter');

// ...

// Add voter
expressVoter.addVoter({
    roles: ['view', 'edit'],
    supports: function(role, subject) {

        // Check if role is in voter roles
        if (!this.roles.find((r) => r.toLowerCase() === role.toLowerCase())) {
            return false;
        }

        // Do other check, if subject instance of SomeThing for example
        // ...

        return true;
    },
    validate: function(role, subject, user, callback) {
        // Validate by role
        switch(role){
            case this.roles[0]:

                // ... 

                // Validation OK
                return callback(null, true);
        }
        callback(new Error('this code should not be reached'));
    }
});

/*
// Or
expressVoter.addVoters([
    {
        "name": "voter_A"
        //...
    },{
        "name": "voter_B"
        // ...
    }
]);
*/

// Apply middleware
app.use(expressVoter());
  1. a) Handle validation from request
app.get('/:subjectId', function(req, res, next){

    // Get subject from "subjectId" parameter
    const subject = {};

    req.validateVoters('view', subject, function(err){
        if(err){
            // One or more voters are not valid
            return next(err);
        }

        // Go on
        // ...
    })
});
  1. b) Handle validation from middleware

const subjectGetter = function(req, callback){

    // Get subject from "subjectId" parameter
    const subject = {};

    callback(null, subject);
}

app.get('/:subject', expressVoter.validate('view', subjectGetter), function(req, res, next){

    // Go on
    // ...

});

Voter configuration

| Key | Type | Required | Default | Description | | --- | --- | --- | --- | --- | | name| string | no | 'voter_${index}' | The voter name | | roles | array<string> | yes | | An array of roles for the voter | | supports | function | yes | | The supports function to know if the voter supports role and subject. Must return true if role and subject are supported by the voter. Pass two arguments, the role and the subject to check | | validate | function | yes | | The validate function to know if the current user is granted. This function is called if the supports function return true. Voter pass the validation function if you call callback like "callback(null, true)". Pass four arguments: role, subject, the current user and the callback function | | errorText | string | no | 'ACCESS_DENIED' | The voter error text when not valid |

Global configuration

app.use(expressVoter({
    // ...
    requestUserKey: 'user'
}));

| Key | Type | Required | Default | Description | | --- | --- | --- | --- | --- | | onNoVoters | function | no | | A function to handle on no voters found | | onNoUser | function | no | | A function to handle on user is not found from "request.${requestUserKey}" | | formatError | function | no | function(){...} | A function to format error on voters not valid | | requestUserKey | string | no | 'user' | The request user key to find current user |