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 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] (https://www.npmjs.com/package/redis) client.
Npm
npm install jwt-redis
Yarn
yarn add jwt-redis
This library is quite fresh, and maybe has bugs. Write me an email to natashkinsash@gmail.com and I will fix the bug in a few working days.
var redis = require('redis');
var JWTR = require('jwt-redis').default;
//ES6 import JWTR from 'jwt-redis';
var redisClient = redis.createClient();
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);
});
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)
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.
You can destroy the token through jti.
await jwtr.destroy(jti)
All methods except the decode method (since it is synchronous) can return a native Promise.
jwtr
.sign({}, secret)
.then(function (token) {
})
.catch(function (err) {
})
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) {
})
Method for creating a token.
Method for verifying a token
Method for breaking the token
Method for decoding token
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);
This library have typing in module.