Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hokaccha/node-jwt-simple
JWT(JSON Web Token) encode and decode module for node.js
https://github.com/hokaccha/node-jwt-simple
Last synced: 1 day ago
JSON representation
JWT(JSON Web Token) encode and decode module for node.js
- Host: GitHub
- URL: https://github.com/hokaccha/node-jwt-simple
- Owner: hokaccha
- License: mit
- Created: 2012-01-24T05:04:37.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2022-07-15T10:19:26.000Z (over 2 years ago)
- Last Synced: 2024-12-19T01:03:24.217Z (9 days ago)
- Language: JavaScript
- Homepage:
- Size: 67.4 KB
- Stars: 1,364
- Watchers: 31
- Forks: 135
- Open Issues: 34
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# jwt-simple
[JWT(JSON Web Token)](http://self-issued.info/docs/draft-jones-json-web-token.html) encode and decode module for node.js.
## Install
$ npm install jwt-simple
## Usage
```javascript
let jwt = require('jwt-simple');
let payload = { foo: 'bar' };
let secret = 'xxx';// HS256 secrets are typically 128-bit random strings, for example hex-encoded:
// let secret = Buffer.from('fe1a1915a379f3be5394b64d14794932', 'hex')// encode
let token = jwt.encode(payload, secret);// decode
let decoded = jwt.decode(token, secret);
console.log(decoded); //=> { foo: 'bar' }
```### decode params
```javascript
/*
* jwt.decode(token, key, noVerify, algorithm)
*/// decode, by default the signature of the token is verified
let 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
let 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)
let 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`.
```javascript
// encode using HS512
jwt.encode(payload, secret, 'HS512')
```