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 🙏

© 2026 – Pkg Stats / Ryan Hefner

hapi-auth-cookie-jwt

v3.0.1

Published

JWT Cookie authentication plugin

Readme

hapi-auth-cookie-jwt

Build Status

This is a Hapi JSON Web Token (JWT), authentication plugin. It's based on the project hapi-auth-jwt, but with the difference that in this plugin, the token is passed through the cookie header field.

JSON Web Token authentication requires verifying a signed token. The 'jwt-cookie' scheme takes the following options:

  • key - (required) The private key the token was signed with.
  • validateFunc - (optional) validation and user lookup function with the signature function(token, callback) where:
    • token - the verified and decoded jwt token
    • callback - a callback function with the signature function(err, isValid, credentials) where:
      • err - an internal error.
      • isValid - true if the token was valid otherwise false.
      • credentials - a credentials object passed back to the application in request.auth.credentials. Typically, credentials are only included when isValid is true, but there are cases when the application needs to know who tried to authenticate even when it fails (e.g. with authentication mode 'try').

See the example folder for an executable example.


var Hapi = require('hapi'),
    jwt = require('jsonwebtoken'),
    server = new Hapi.Server();

server.connection({ port: 8080 });

var accounts = {
    123: {
        id: 123,
        user: 'john',
        fullName: 'John Doe',
        scope: ['a', 'b']
    }
};

var privateKey = 'FCC064A9-204E-4E58-A44B-B545D7FD077Y';

// Use this token to set your cookie header.
// Ex:
// set-cookie/cookie: access_token=<token>
var token = jwt.sign({ accountId: 123 }, privateKey);

var validate = function (decodedToken, callback) {

    var error = null;
    var credentials = accounts[decodedToken.accountId] || {};

    if (!credentials) {
        return callback(error, false, credentials);
    }

    return callback(error, true, credentials)
};

var doLoginHandler = function(request, reply) {
	
	// Do whatever is necessary to check user authenticity
	//...
	// and then set-cookie to the client
	return reply().state('access_token', token);
};

server.register(require('hapi-auth-cookie-jwt'), function (error) {

    server.auth.strategy('token', 'jwt-cookie', {
        key: privateKey,
        validateFunc: validate
    });
    
	// Setting the authentication cookie on login
	server.route({
		method: 'POST',
		path: '/login',
		handler: doLoginHandler
	});

    server.route({
        method: 'GET',
        path: '/',
        handler: function(request, reply) {
            return reply('/ -> ok!');
        },
        config: {
            auth: 'token'
        }
    });

    // With scope requirements
    server.route({
        method: 'GET',
        path: '/withScope',
        handler: function(request, reply) {
            return reply('/withScope -> ok!');
        },
        config: {
            auth: {
                strategy: 'token',
                scope: ['a']
            }
        }
    });
});

server.start();