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-jwt-couch

v4.3.0

Published

Simple user authentication for Hapi server using JWT and storing user data and encrypted passwords in couchdb.

Downloads

57

Readme

hapi-jwt-couch

Hapi plugin to validate users using hapi-auth-jwt, storing user information and encrypted passwords in a couchdb instance.

This plugin also provides a 'recover my password' option by setting up an email account using nodemailer.

Edit the "message" portion of the configuration. The strings @USERNAME@, @SERVER@ and @TOKEN@ are replaced before sending the email.

Usage


npm install hapi-jwt-couch

Hapi plugin

The values "user", "password" and "login" are optional. The default values are shown in this example.


const Hapi = require('hapi');
cont Joi = require('@hapi/joi');

var password = Joi.string().regex(/^(?=.*[\d])(?=.*[A-Z])(?=.*[a-z])[\w\d!@#$%_-]{6,40}$/);

var hapijwtcouch = {};
hapijwtcouch.register = require("hapi-jwt-couch");
hapijwtcouch.options = {
        "privateKey": "SomeRandomKey123",
        "saltRounds": 10,
        "algorithm": { 
            "algorithm": "HS256",
            "expiresIn": "7d"
        },
        "validateOptions": { 
            "algorithms": [ "HS256" ] 
        },
        "mailer": {
            "nodemailer": {
				host: 'smtp.gmail.com',
			    port: 465,
			    secure: true, // use SSL
			    auth: {
			        user: '[email protected]',
			        pass: 'pass'
			    }
			},
			"from": "Hapi jwt couch <[email protected]>",
			"message": "Hello @USERNAME@,<br>Somebody asked me to send you a link to reset your password, hopefully it was you.<br>Follow this <a href='@SERVER@/public/#/login/reset?token=@TOKEN@'>link</a> to reset your password.<br>The link will expire in 30 minutes.<br>Bye.",
			"uri": "http://your.public.ip"
        },
        "userdb" : {
            "hostname": "http://localhost:5984",
            "database": "hapijwtcouch"
        },
        "password" = password,
        "user" = Joi.object().keys({
	        "name": Joi.string().required(),
	        "email": Joi.string().email().required(),
	        "password": password
        }),
        "login": Joi.object().keys({
	        "email": Joi.string().email().required(),
	        "password": password
	    })
    };
    

var hapiauth = {};
hapiauth.register = require("hapi-auth-jwt");
hapiauth.options = {};


var plugins = [hapiauth, hapijwtcouch];

var server = new Hapi.Server();
server.connection({ 
    port: "3000"
});

server.register(plugins, function(err){
    if (err) {
        throw err; // something bad happened loading the plugin
    }

    server.start(function (err) {

        console.log("server running", server.info.uri);
        
    });
});

Create your own Hapi plugin and extend it with your own validation function

You can extend this plugin by adding your own validation function. You may also change the validation for user, password and login Joi objects.

The Joi objects shown here for password, user and login are used by default.


const Promise = require('bluebird');

exports.register = function (server, conf, next) {

	//The validation function has this signature and the return value must be a Promise. 
	const validate = function(req, decodedToken){
		//validate your decoded token, the resulting object must have the field 'scope'
		if(validationTrue){
			return Promise.resolve({
				"scope": ["custom_scope"]
				});
		}else{
			return Promise.reject("Not validated");
		}
	}

	try{
		server.methods.jwtauth.addValidationFunction(validate);	
	}catch(e){
		console.error(e);
	}
	
	//Additional logic for your plugin

	return next();
	
};

exports.register.attributes = {
  pkg: require('./package.json')
};

Testing

Start the server. The script test starts the server and adds the plugin.


npm test

Run the tests using hapi-jwt-couch-lib

The test for this package are in hapi-jwt-couch-lib


npm test