@fntools/jsonwebtoken
TypeScript icon, indicating that this package has built-in type declarations

0.0.8 • Public • Published

@fntools/jsonwebtoken is a class based jsonwebtoken alternative which is created from jsonwebtoken library itself.

INSTALL

# NPM
$ npm install @fntools/jsonwebtoken --save

// or 
# YARN 
$ yarn add @fntools/jsonwebtoken

DOCUMENTATION/USAGE

new JWT(secret:string|PrivateKey)

  • secret it is a string or a privateKey that is used to sign the jwt token

METHODS

  • sign(payload[,options][,cb(error,token)]) is the method used to create the jwt token string

    • payload - is an object leteral that you want to sign
    • options - is jwt options
    • cb - is the callback funtion only used if dev doesn't want to use promise
  • verify(token[,options][,cb(error,decode)]) is a method that is used verify a token.

    • token - is a jwt token string
    • options - is jwt options
    • cb - is the callback funtion only used if dev doesn't want to use promise

new Jsonwebtoken(secret:string|PrivateKey)

It is a class who behave like the original jwt

  • secret it is a string or a privateKey that is used to sign the jwt token

METHODS

  • sign(payload[,options][,cb(error,token)])
    • payload - is an object leteral that you want to sign
    • options - is jwt options
    • cb - is the callback is used to let jwt behave asyncronously
  • verify(token[,options][,cb(error,decode)])
    • payload - is an object leteral that you want to sign
    • options - is jwt options
    • cb - is the callback is used to let jwt behave asyncronously

Note JWT.prototype.sync === new Jsonwebtoken(someSecret)

Examples

const { JWT } = require("@fntools/jsonwebtoken"); // or import {Jsonwebtoken} from "@fntools/jsonwebtoken"
// initialize
const jwt = new JWT("some secret");
jwt.sign({ sample: "sample payload" })
  .then((token) => {
    // do something cool 
  })
  .catch((error) => {
    // handles error 
  });

// or use callback for synchrounous call 
jwt.sign({ sample: "sample payload" }, (error, token) => {
  if (error) {
    // do something cool 
  } else {
    // handle error 
  }
});


const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzYW1wbGUiOiJzYW1wbGUgcGF5bG9hZCIsImlhdCI6MTY3MDY3NDQ3Nn0.g8bh59r5qwB6u-Lb5X38R_wr5xELUpdqVffRIwoyc9c";

jwt.verify(token)
  .then((decode) => {
    // do something cool...
  })
  .catch((error) => {
    // handles error
  });

// or verify sync  using cb
jwt.verify(token, (error, decode) => {
  if (error) {
    // handle errors
  } else {
    // handle success
  }
});
const {Jsonwebtoken} = require("@fntools/Jsonwebtoken"); // import Jsonwebtoken from "@fntools/Jsonwebtoken"
// if you want @fntools/jsonwebtoken to behave like the jsonwebtoken original library you will use Jsonwebtoken or JWT.sync ;

const secret = "some secret";
const jwt = new Jsonwebtoken(secret);
// sign
var token = jwt.sign({ foo: 'bar' });
// if callback is not provided it will behave synchronously and will return jwt string
// sign  asynchronously
jwt.sign({ foo: 'bar' }, { algorithm: 'RS256' }, function(err, token) {
  // do something call 
});


// verify a token symmetric - synchronous
var decoded = jwt.verify(token);
console.log(decoded.foo) // bar

// verify a token symmetric
jwt.verify(token,  function(err, decoded) {
  console.log(decoded.foo) // bar
});

JWT.prototype.sync

const {JWT} = require("@fntools/jsonwebtoken") // or import {JWT} from "@fntools/j"
const secret = "some secret"
const jwt = new JWT('some secret');
// sign
var token = jwt.sync.sign({ foo: 'bar' });
// if callback is not provided it will behave synchronously and will return jwt string
// sign  asynchronously
jwt.sync.sign({ foo: 'bar' }, { algorithm: 'RS256' }, function(err, token) {
  console.log(token);
});
// verify
 verify a token symmetric - synchronous
var decoded = jwt.verify(token);
console.log(decoded.foo) // bar

// verify a token symmetric
jwt.sync.verify(token,  function(err, decoded) {
  console.log(decoded.foo) // bar
});

Note new JWT(secret).sync === jwt and Jsonwebtoken is better for non-async manner it doesn't require the use of async and await

Learn More @t jsonwebtoken

Readme

Keywords

none

Package Sidebar

Install

npm i @fntools/jsonwebtoken

Weekly Downloads

2

Version

0.0.8

License

MIT

Unpacked Size

31.2 kB

Total Files

12

Last publish

Collaborators

  • loweandaya7566