@dsninjas/jwt

Jwt Helper for node.js projects

Stats

StarsIssuesVersionUpdatedCreatedSize
@dsninjas/jwt
1.0.04 years ago4 years agoMinified + gzip package size for @dsninjas/jwt in KB

Readme

ds-util-jwt

Jwt Helpers for node.js projects

This module makes it easy to issue and verify tokens in node.js applications.

Installation

  • Make sure that Node.js installed.
  • Install Node.js library with npm:
    npm install @dsninjas/jwt
    
  • You can also install Node.js library with yarn:
    yarn add @dsninjas/jwt
    

Usage

constructor(secret, audience, issuer)

The constructor helps you configure the jwt helper class.

const secret = 'qwerty';
const audience = 'example.com';
const issuer = 'example.com';
const jwtHelper = new JwtHelper(secret, audience, issuer);

The constructor takes the following options:

  1. secret (required): this is the secret used to create the .
  2. audience (required): this is used to check the audience.
  3. issuer (required): this is the isser of the token.

issue(payload, expirytime, subject, cb)

This method helps you to generate the token. It responses with a promise if you don't secify a callback, else returns the result with in callback.

With promise:

const secret = 'qwerty';
const audience = 'example.com';
const issuer = 'example.com';
const expireIn = '7d';
const jwtHelper = new JwtHelper(secret, audience, issuer);
const message = 'Hello';
const result = jwtHelper.issue({ message }, expireIn);
result.then((result) => {
    console.log(result);
}.catch((err) => {
    console.log(err);
});

With callback:

const secret = 'qwerty';
const audience = 'example.com';
const issuer = 'example.com';
const expireIn = '7d';
const jwtHelper = new JwtHelper(secret, audience, issuer);
const message = 'Hello';
jwtHelper.issue({ message }, expireIn,'jwt-auth-token',(err,result) => {
    if (err) {
        console.log(err);
    }
    console.log(result);
}
  1. payload (required): this is the data you want to encrypt.
  2. expirytime (required): this is time the token woould expire.
  3. subject (optional): this is the subject of the jwt.
  4. cb (optional): this is the callback the woulld be called with the response.

Sample response:

{ 
  uuid: '4c71ae9d-755a-4d18-9eef-4a600bfb0c14',
  token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiNmMkNU0iLCJpYXQiOjE1MTYxOTU3NTQsImV4cCI6MTUxNjgwMDU1NCwiYXVkIjoiaHR0cDovL251bi52Yy9zaW13aSIsImlzcyI6Imh0dHA6Ly9udW4udmMvc2ltd2kiLCJzdWIiOiJqd3QtYXV0aC10b2tlbiIsImp0aSI6IjRjNzFhZTlkLTc1NWEtNGQxOC05ZWVmLTRhNjAwYmZiMGMxNCJ9.khIFuxlb4mF8iZnRc_qbXfzjbzd0XNY7CKauSCcKB6U' 
}

verify(token, cb)

This method helps you to validate the token. It responses with a promise if you don't secify a callback, else returns the result with in callback.

With promise:

const secret = 'qwerty';
const audience = 'example.com';
const issuer = 'example.com';
const expireIn = '7d';
const jwtHelper = new JwtHelper(secret, audience, issuer);
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiNmMkNU0iLCJpYXQiOjE1MTYxOTU3NTQsImV4cCI6MTUxNjgwMDU1NCwiYXVkIjoiaHR0cDovL251bi52Yy9zaW13aSIsImlzcyI6Imh0dHA6Ly9udW4udmMvc2ltd2kiLCJzdWIiOiJqd3QtYXV0aC10b2tlbiIsImp0aSI6IjRjNzFhZTlkLTc1NWEtNGQxOC05ZWVmLTRhNjAwYmZiMGMxNCJ9.khIFuxlb4mF8iZnRc_qbXfzjbzd0XNY7CKauSCcKB6U';

const result = jwtHelper.verify(token, expireIn);
result.then((result) => {
    console.log(result);
}.catch((err) => {
    console.log(err);
});

With callback:

const secret = 'qwerty';
const audience = 'example.com';
const issuer = 'example.com';
const expireIn = '7d';
const jwtHelper = new JwtHelper(secret, audience, issuer);
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiNmMkNU0iLCJpYXQiOjE1MTYxOTU3NTQsImV4cCI6MTUxNjgwMDU1NCwiYXVkIjoiaHR0cDovL251bi52Yy9zaW13aSIsImlzcyI6Imh0dHA6Ly9udW4udmMvc2ltd2kiLCJzdWIiOiJqd3QtYXV0aC10b2tlbiIsImp0aSI6IjRjNzFhZTlkLTc1NWEtNGQxOC05ZWVmLTRhNjAwYmZiMGMxNCJ9.khIFuxlb4mF8iZnRc_qbXfzjbzd0XNY7CKauSCcKB6U';

const result = jwtHelper.verify(token, expireIn,'jwt-auth-token',(err,result) => {
    if (err) {
        console.log(err);
    }
    console.log(result);
}
  1. token (required): this is the data you want to encrypt.
  2. cb (optional): this is the callback the woulld be called with the response.

Sample response:

{ 
  message: '6c$5M',
  iat: 1516195754,
  exp: 1516800554,
  aud: 'http://nun.vc/simwi',
  iss: 'http://nun.vc/simwi',
  sub: 'jwt-auth-token',
  jti: '4c71ae9d-755a-4d18-9eef-4a600bfb0c14' 
}

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.