JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 403
  • Score
    100M100P100Q102119F

JSON Web Token (JWT) authentication plugin

Package Exports

  • hapi-auth-jwt

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hapi-auth-jwt) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

hapi-auth-jwt

hapi JSON Web Token (JWT) authentication plugin

Build Status

JSON Web Token authentication requires verifying a signed token. The 'jwt' 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 accounts = {
    123: {
      id: 123,
      user: 'john',
      name: 'John Doe',
      scope: ['a', 'b']
    }
};

var validate = function (decodedToken, callback) {

    var account = accounts[decodedToken.accountID];
    if (!user) {
        return callback(null, false);
    }

    callback(err, isValid, {id: account.id, name: account.name });
};

server.pack.require('hapi-auth-jwt', function (err) {
    var privateKey = 'BbZJjyoXAdr8BUZuiKKARWimKfrSmQ6fv8kZ7OFfc';

    server.auth.strategy('token', 'jwt', { key: privatekey,  validateFunc: validate });
    server.route({ method: 'GET', path: '/', config: { auth: 'token' } });
});