Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

jwt-simple

hokaccha987.9kMIT0.5.6TypeScript support: included

JWT(JSON Web Token) encode and decode module

jwt, encode, decode

readme

jwt-simple

JWT(JSON Web Token) encode and decode module for node.js.

Install

$ npm install jwt-simple

Usage

var jwt = require('jwt-simple');
var payload = { foo: 'bar' };
var secret = 'xxx';

// HS256 secrets are typically 128-bit random strings, for example hex-encoded:
// var secret = Buffer.from('fe1a1915a379f3be5394b64d14794932', 'hex')

// encode
var token = jwt.encode(payload, secret);

// decode
var decoded = jwt.decode(token, secret);
console.log(decoded); //=> { foo: 'bar' }

decode params

/*
 * jwt.decode(token, key, noVerify, algorithm)
 */

// decode, by default the signature of the token is verified
var decoded = jwt.decode(token, secret);
console.log(decoded); //=> { foo: 'bar' }

// decode without verify the signature of the token,
// be sure to KNOW WHAT ARE YOU DOING because not verify the signature
// means you can't be sure that someone hasn't modified the token payload
var decoded = jwt.decode(token, secret, true);
console.log(decoded); //=> { foo: 'bar' }

// decode with a specific algorithm (not using the algorithm described in the token payload)
var decoded = jwt.decode(token, secret, false, 'HS256');
console.log(decoded); //=> { foo: 'bar' }

Algorithms

By default the algorithm to encode is HS256.

The supported algorithms for encoding and decoding are HS256, HS384, HS512 and RS256.

// encode using HS512
jwt.encode(payload, secret, 'HS512')

changelog

0.5.6

Remove usage of deprecated Buffer constructor #89 @eliot-akira

0.5.4

Excludes coverage/ in .npmignore

0.5.3

Security fix #86

0.5.2

Add types #83

0.5.0

Add support for nbf and exp claims #38 @alexjab

0.4.1

Fix version numver #32

0.4.0

add extra jwt header function to encode #28

0.3.1

  • Check for lack of token #23

0.3.0

  • Add an algorithm parameter to the decode method #16 @gregorypratt

0.2.0

  • Add the RS256 alg #7 [thedufer]