@chantouchsek/jwt-redis
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

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.

Build Status Latest Version on NPM Software License npm npm

Installation

Npm

npm install @chantouchsek/jwt-redis

Yarn

yarn add @chantouchsek/jwt-redis

Support

This library is quite fresh, and maybe has bugs. Write me an email to chantouchsek.cs83@gmail.com and I will fix the bug in a few working days.

Quick start

const redis = require('redis')
const JWTR =  require('@chantouchsek/jwt-redis').default 
//ES6 import JWTR from '@chantouchsek/jwt-redis';
const redisClient = redis.createClient()
const jwtr = new JWTR(redisClient)

const secret = 'secret'
const jti = 'test'
const 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.

    const 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.

try {
  const token = await jwtr.sign({}, secret)
  console.log(token);
} catch (e) {
  console.log(e)
}

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.

    const Promise = require('bluebird')
    const Redis = require('ioredis')
    const redis = new Redis()
    const JWTR =  require('@chantouchsek/jwt-redis')
    //ES6 import JWTR from 'jwt-redis';
    const jwtr = new JWTR(redis)
    const 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:.

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

TypesScript

This library have typing in module.

Package Sidebar

Install

npm i @chantouchsek/jwt-redis

Weekly Downloads

0

Version

0.0.1

License

ISC

Unpacked Size

21 kB

Total Files

14

Last publish

Collaborators

  • chantouchsek