4.0.1 • Published 12 months ago

jsonwebtoken_1 v4.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

Installation

 $ npm install jsonwebtoken_1

Usage

jwt.sign(payload, secretOrPrivateKey, options, callback)

Import in node.js file

const jwt = require('jsonwebtoken_1')
         //or
import jwt from ('jsonwebtoken_1')

---must specify the algorithm as 'HS256' for Symmetric and 'RS256' or 'ES256' for asymmetric in options.

jwt.sign(payload, secretOrPrivateKey, {options}) secretOrPrivateKey is a string (utf-8 encoded), buffer, object, or KeyObject containing either the secret for HMAC algorithms or the PEM encoded private key for RSA and ECDSA. In case of a private key with passphrase an object { key, passphrase } can be used (based on crypto documentation), in this case be sure you pass the algorithm. When signing with RSA algorithms the minimum modulus length is 2048 except when the allowInsecureKeySizes option is set to true. Private keys below this size will be rejected with an error.

Sign with (HMAC SHA256)

const jwt = require('jsonwebtoken_1');
var token = jwt.sign({name: 'suman' }, 'secret',{ expiresIn : '15m',algorithm:'HS256'});

sign with RSA SHA256

const jwt = require('jsonwebtoken_1');
var privateKey = fs.readFileSync('private.key');// get private key should be pem file
var token = jwt.sign({ name: 'suman' }, privateKey, { expiresIn : '15m',algorithm:'RS256'});

sign with ES256

const jwt = require('jsonwebtoken_1');
var privateKey = fs.readFileSync('private.key');// get private key should be pem file
var token = jwt.sign({ name: 'suman' }, privateKey, { expiresIn : '15m',algorithm:'ES256'});

verify a token symmetric

jwt.verify(token, 'secret',function(err, decoded) {
  console.log(decoded.name) // suman
});

verify a token asymmetric

var publickey = fs.readFileSync('public.key');  // get public key should be pem file
jwt.verify(token, publickey,function(err, decoded) {
  console.log(decoded.name) // suman
});
3.0.8

12 months ago

3.0.7

12 months ago

3.0.6

12 months ago

4.0.1

12 months ago

4.0.0

12 months ago

3.0.9

12 months ago

3.0.5

12 months ago

3.0.4

12 months ago

3.0.3

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.0.6

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.3

1 year ago