JSPM

jwthelper

0.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 320
  • Score
    100M100P100Q94493F
  • License MIT

Helper for easy consumption of JSON Web Tokens

Package Exports

  • jwthelper

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 (jwthelper) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

jwthelper

An easy to use helper class for jsonwebtoken.

Install

npm install jwthelper --save

Usage

var helper = JWTHelper.createJWTHelper([options]);
  • options:
    • algorithm: an algorithm for signing supported by jsonwebtoken (default HS512)

    • secret: a string containing the secret to encode the token with

    • privateKey: a private key to encode the token with

    • expiresInMinutes or expiresInSeconds: set jwt expiration

    • noTimestamp: omit the timestamp in the jwt payload

    • 'ignoreExperiation': ignore expiration settings when verifying a jwt

    • audience: set the audience for signing and verifying

    • issuer: set the issuer for signing and verifying

    • subject: set the subject for signing and verifying

    • headers: an optional array containig header fields

      var jwt = helper.sign(payload[, signOptions]);

Signs a JWT with the payload, The optional signOptions accept all options mentioned above and will be used for this signing only.

helper.verify(token, [verifyOptions, ]callback);

Verifies a token. The callback conforms to the standard (error, payload) scheme. verifyOptions accepts all optios mentioned above and will be used for this verification only.

helper.setOptions(options);

Updates the options for the helper. The options array accepts all parameters mentioned above.

Example

var JWTHelper = require('jwthelper');

// Create a new helper and set the options for that helper
var helper = JWTHelper.createJWTHelper({
    'secret': 'this is my secret'
});

// Signing a JWT
var jwt = helper.sign({
    '_id': 55,
    'isAdmin': true
});

// This outputs the signed JWT
console.log(jwt);

// Now we are going to decode it!
helper.verify(jwt, function(err, decoded) {
    if(err) return console.log((err.name == 'JsonWebTokenError') ? 'Invalid token' : err.name);
    // And we display the decoded JWT
    console.log(decoded);
});

Version history

  • 0.0.1 - 12 July 2015
    • First release

License

Copyright 2015 Michiel van der Velde.

This software is licensed under the MIT License