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 🙏

© 2025 – Pkg Stats / Ryan Hefner

hapi-jot

v1.0.4

Published

JSON Web Token authentication plugin for hapi

Downloads

2

Readme

jot Logo

JWT (JSON Web Token) authentication plugin for hapi.

Build Status

Documentation

API Documentation

Example

'use strict';

const Hapi = require('hapi');
const Hoek = require('hoek');

const users = {
   jappleseed: {
       id: 1,
       password: 'loveThoseApples', // 'secret'
       name: 'Johnny Appleseed',
       username: 'jappleseed'
   }
};

const server = new Hapi.Server();

server.connection({ host: 'localhost', port: 3000 });

// Register jot with the server
server.register(require('hapi-jot'), (err) => {

   // Handle jot validation errors
   Hoek.assert(!err, err);

   // Declare an authentication strategy using the jwt scheme
   // with the secret key to sign and verify the JWT with, 
   // and the validation function to validate user credentials.
   //
   // The strategy exposes a '/token' endpoint to generate a JWT.
   // A POST request to '/token' with { username: 'jappleseed, password: 'apples' }
   // will respond with a token in the format of:
   // { token: 'eyJ0eXAiOiJKV1QiLCJhbGciO...' }
   server.auth.strategy('token', 'jwt', {
       key: 'mySuperSecretKey',
       validateFunc: (request, callback) => {

           // access the request parameters
           const username = request.payload.username;
           const password = request.payload.password;

           const user = users[username];
           if (!user) {
               return callback(null, false);
           }

           // Perform the validation, obviously
           // passwords shouldn't be stored in plain-text
           // in practice.
           if (user.password === password) {

               callback(null, true, { id: user.id, username: user.username });
           }
           else {

               callback(null, false);
           }


       }
   });


   // Use the 'token' authentication strategy to protect the
   // endpoint that gets the authenticate user's name.
   //
   // In order to authenticate with this endpoint, a valid token
   // must be sent in the 'Authorization' header of the request.
   // 
   // The header takes the form of:
   // 'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciO...'
   server.route({
       method: 'GET',
       path: '/user',
       config: {
           auth: {
               strategy: 'token'
           },
           handler: function (request, reply) {

               if (!request.auth.isAuthenticated) {
                   return reply('Authentication failed due to: ' + request.auth.error.message);
               }

               // Perform the account lookup by using the credentials
               // stored in request.auth.credentials.
               const user = users[request.auth.credentials.username];

               return reply({ name: user.name });
           }
       }
   });

   server.start((err) => {

       Hoek.assert(!err, err);
       console.log(`Server started at: ${server.info.uri} \n\n`);

       console.log(`1.  To generate a token.`);
       console.log(`POST to ${server.info.uri}/token with the following JSON:\n`);

       console.log({
           username: 'jappleseed',
           password: 'loveThoseApples'
       });

       console.log(`\n\n2. To access the secured route include the token as a header in the request.`);

       console.log(`GET to ${server.info.uri}/user with the following header:\n`);

       console.log({
           'Authorization': 'Bearer (token value)'
       });
   });
});