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.
npm install jwt-redis
yarn add jwt-redis
This library is quite fresh, and maybe has bugs. Write me an email to email@example.com and I will fix the bug in a few working days.
var redis = ;var JWTR = default;//ES6 import JWTR from 'jwt-redis';var redisClient = redis;var jwtr = redisClient;var secret = 'secret';var jti = 'test';var payload = jti ;// Create a tokenjwtr;
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 validawait jwtr// exp - time at which the token will not be validawait jwtr
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
If jti is not present, then jti is generated randomly by the library.
You can destroy the token through jti.
All methods except the decode method (since it is synchronous) can return a native Promise.
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 = ;var Redis = ;var redis = ;var JWTR = ;var jwtr = redis;var jwtrAsync = Promise;jwtrAsync
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 = redis options;
This library have typing in module.