npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fntools/jsonwebtoken

v0.0.8

Published

Use JSON-WEB-TOKEN with some extended feature

Downloads

12

Readme

@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