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

jwt-redis

v7.0.3

Published

This library completely repeats the entire functionality of the library [jsonwebtoken](https://www.npmjs.com/package/jsonwebtoken), with one important addition. Jwt-redis allows you to store the token label in redis to verify validity. The absence of a to

Downloads

3,300

Readme

jwt-redis

This library completely repeats the entire functionality of the library jsonwebtoken, with one important addition. Jwt-redis allows you to store the token label in redis to verify validity. The absence of a token label in redis makes the token not valid. To destroy the token in jwt-redis, there is a destroy method. This makes it possible to make a token not valid until it expires. Jwt-redis support node_redis client.

Installation

Npm

npm install jwt-redis

Yarn

yarn add jwt-redis

Support

This library is quite fresh, and maybe has bugs. Write me an email to [email protected] and I will fix the bug in a few working days.

Quick start

var redis = require('redis');
var JWTR =  require('jwt-redis').default;
//ES6 import JWTR from 'jwt-redis';
var redisClient = redis.createClient();
await redisClient.connect();
var jwtr = new JWTR(redisClient);

var secret = 'secret';
var jti = 'test';
var payload = { jti };

// Create a token
jwtr.sign(payload, secret)
    .then(()=>{
            // Token verification
            return jwtr.verify(token, secret);
    })
    .then(()=>{
            // Destroying the token
            return jwtr.destroy(jti, secret);
    });

Expiration time

You can set the lifetime of the token the same way as in the jsonwebtoken library. The label in redis is deleted when the token expires.

    // expiresIn - number of seconds through which the token will not be valid
    await jwtr.sign({}, 'secret', {expiresIn: expiresIn})
    // exp - time at which the token will not be valid
    await  jwtr.sign({exp: exp}, secret)

Create jti

For each token, the claims are added jti. Jti is the identifier of the token. You can decide for yourself what it will be equal by adding its values to payload.

    var payload = {jti: 'test'};
    await jwtr.sign(payload, secret)

If jti is not present, then jti is generated randomly by the library.

Destroy token

You can destroy the token through jti.

    await jwtr.destroy(jti)

Native Promise

All methods except the decode method (since it is synchronous) can return a native Promise.

    jwtr
    .sign({}, secret)
    .then(function (token) {

    })
    .catch(function (err) {

    })

Bluebird

If you want to use Bluebird, then after the promiscilation all the methods of the library will be available that return Promise, Only at the end of each method should you add Async.

    var Promise = require('bluebird');
    var Redis = require('ioredis');
    var redis = new Redis();
    var JWTR =  require('jwt-redis');
    var jwtr = new JWTR(redis);

    var jwtrAsync = Promise.promisifyAll(jwtr);

    jwtrAsync
    .signAsync({}, secret)
    .then(function (token) {

    })
    .catch(function (err) {

    })

API

Method for creating a token.

jwtr.sign(payload, secretOrPrivateKey, [options]): Promise

Method for verifying a token

jwtr.verify(token, secretOrPublicKey, [options]): Promise

Method for breaking the token

jwtr.destroy(jti, [options]): Promise

Method for decoding token

jwt.decode(token, [options]): T

jwt-redis fully supports all method options that support the library jsonwebtoken. Therefore, it is better to read their documentation in addition. But there are several options that are available only in jwt-redis.

Also in the options you can specify a prefix for the redis keys. By default it is jwt_label:.

var options = {
    prefix: 'example'
}
var jwtr = new JWTR(redis, options);

TypesScript

This library have typing in module.