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

hapi-usergen-mongo

v2.0.0

Published

MongoDB user generator for Hapijs

Downloads

5

Readme

hapi-usergen-mongo

hapi MongoDB user generator

Install

npm install hapi-usergen-mongo

Changes from v1 to v2

All user register function no longer take username and password arguments. Instead they take a object. The object requires name and pwd keys as follows {'name': username, 'pwd': password}, for registerCr those are the only fields that should be used. For registerLocal/Github you can add in any additional fields you would like.

registerCr no longer requires a value to be passed for roles and customData. Roles must be an array and customData must be a object if passed.

registerLocal/Github now have a data argument for any additional information you would like stored with the user. This field is a object.

Usage

A user generator plugin for hapi and MongoDB. Must have database already set up with one user that has readWrite role if you are storing local or userAdmin if storing CR. Requires options:

  • ip - The IP address of the database. Defaults to 127.0.0.1.
  • port - The port number of the database Defaults to 27017.
  • db - The name of the database. Defaults to test.
  • name and/or userAdmin- name is readWrite user. userAdmin is userAdmin user.
  • pwd and/or userAdminPwd - pwd is readWrite password. userAdminPwd is userAdmin password.
  • ssl - MongoDB ssl. Defaults to false.

Exports functions:

  • user.registerLocal(user, data, callback) - User stored as document. Callback is (err, true). Has schema of {_id: username, local: {name: username, pwd: password}, data: {}}. Password is stored with bcrypt.
  • user.registerLocalGithub(user, data, callback) - Github OAuth credentials stored as document. Callback is (err, true). Has schema of {_id: username, github: {name: username, pwd: token}, data: {}}.
  • user.removeLocal(username, callback) - Removes local or github user. Callback is (err, true).
  • user.registerCr(user, roles, customData, callback) - User stored as MongoDB Challenge-response user. Callback is (err, true).
  • user.removeCr(name, callback) - Removes Challenge-response user. Callback is (err, true).

Some Notes:

  • A user can register local and then register github to link the two. However a user cannot register github then set up a local account. It will cause a error.
  • If both local and challenge-response users are allowed it will not catch duplicate names between the two. Therefore it's recomended to choose between local/github or challenge-response.

Example set up:

var Hapi = require('hapi');

var server = Hapi.createServer('127.0.0.1', 3000);

server.pack.register({
    plugin: require('hapi-usergen-mongo'),
    options: {
        db: 'users',
        name: 'sessionHandler',
        pwd: 'supersecretpassword',
        userAdmin: 'userHandler',
        userAdminPwd: 'supersecretpassword',
        ssl: true
    }
}, function (err) {
    if (err) {
        console.log(err);
    }
});

server.route([
    {
        method: 'POST',
        path: '/registerlocal',
        handler: function (req, res) {
            var user = {'name': req.payload.username, 'pwd': req.payload.password};

            server.plugins['hapi-usergen-mongo'].user.registerLocal(user, function(err, created) {
                if (err) {
                    res(err);
                }

                res(created);
            });
        }
    },
    {
        method: 'POST',
        path: '/registergithub',
        handler: function (req, res) {
            // Getting information from Github for OAuth is up to you.
            // I highly suggest using bell https://github.com/hapijs/bell
            server.plugins['hapi-usergen-mongo'].user.registerGithub({'name': req.payload.username,
            'pwd': req.payload.password}, function(err, created) {
                if (err) {
                    res(err);
                }

                res(created);
            });
        }
    },
    {
        method: 'POST',
        path: '/removelocal',
        handler: function (req, res) {
            server.plugins['hapi-usergen-mongo'].user.removeLocal(req.payload.username,
            function(err, removed) {
                if (err) {
                    res(err);
                }

                res(removed);
            });
        }
    },
    {
        method: 'POST',
        path: '/registercr',
        handler: function (req, res) {
            // Creates a MongoDB user with no roles or customData
            server.plugins['hapi-usergen-mongo'].user.registerCr({'name': req.payload.username,
            'pwd': req.payload.password}, function(err, created) {
                if (err) {
                    res(err);
                }

                res(created);
            });
        }
    },
    {
        method: 'POST',
        path: '/removecr',
        handler: function (req, res) {
            server.plugins['hapi-usergen-mongo'].user.removeCr(req.payload.username,
            function(err, removed) {
                if (err) {
                    res(err);
                }

                res(removed);
            });
        }
    },
]};

server.start();

Testing

Tests are ran using npm test and require the env variables:

  • LOCAL_USER
  • LOCAL_PWD
  • GITHUB_USER
  • USED_USER
  • USED_PWD
  • DATABASE
  • DB_USER
  • DB_PWD
  • USER_ADMIN
  • USER_PWD

with optional env variables:

  • SSL

Future Plans

Add more OAuth options such as twitter, facebook, etc.