JWT(JSON Web Token) encode and decode module for node.js
Branch: master
Clone or download
Latest commit 0840c0c Sep 26, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Specify files to publish Sep 26, 2018
test Add support for nbf and exp claims Feb 21, 2016
.gitignore Improve tests suite: Jan 16, 2016
.travis.yml upgrade travis config for node 4 and node 5 Jan 21, 2016
History.md 0.5.4 Sep 24, 2018
LICENSE Add LICENSE Jan 24, 2012
README.md Readme title update to npm package name Sep 24, 2018
index.d.ts chore(types): add types Sep 17, 2018
index.js create project Jan 24, 2012
package.json Specify files to publish Sep 26, 2018

README.md

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')