- Notifications
You must be signed in to change notification settings - Fork4
Management of sessions by Redis and JWT for horizontal scalability, with the possibility of having one session at a time or multiple for the same user
License
NotificationsYou must be signed in to change notification settings
bytesleo/redis-jwt
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Management of sessions by Redis and JWT for horizontal scalability, with the possibility of having one session at a time or multiple for the same user
Npm
npm install redis-jwt --save
Yarn
yarn add redis-jwt
importRedisJwtfrom'redis-jwt';constr=newRedisJwt({//host: '/tmp/redis.sock', //unix domainhost:'127.0.0.1',//can be IP or hostnameport:6379,// portmaxretries:10,//reconnect retries, default 10//auth: '123', //optional password, if neededdb:0,//optional db selectionsecret:'secret_key',// secret key for Tokens!multiple:false,// single or multiple sessions by userkea:false// Enable notify-keyspace-events KEA});r.sign('507f191e810c19729de860ea').then(token=>{r.verify(token).then(decode=>{// [Object]}).catch(err=>{// Wrong token});});
importRedisJwtfrom'redis-jwt';importexpressfrom'express';constr=newRedisJwt();constapp=express();// Loginapp.get('/login',(req,res)=>{r.sign('507f191e810c19729de860ea',{ttl:'15 minutes',dataToken:{// Publichello:'world'},dataSession:{// Privatehello:'world',headers :req.headers}}}).then(token=>{res.json({token});});});// Meapp.get('/me',mw(),(req,res)=>{res.json(req.user);});// Middlewarefunctionmw(){return(req,res,next)=>{consttoken=req.headers['authorization'];r.verify(token).then(decode=>// here you can get user from DB by id (decode.id)req.user=decode;next();}).catch(err=>{res.status(401).json({err})})}}app.listen(3000,()=>console.log('Server listening on port 3000!'));
// Basicr.sign('507f191e810c19729de860ea').then..// TTL : 50 seconds, 10 minutes, 5 hours, 3 days, 1 year ...r.sign('507f191e810c19729de860ea',{ttl:'15 minutes'}).then...// Save data in token : Object are saved in tokenr.sign('507f191e810c19729de860ea',{dataToken:{world:'hello'}}).then...// Save data in redis : Object are saved in redis-jwtr.sign('507f191e810c19729de860ea',{dataSession:{hello:'world'}}).then...// Example TTL + dataToken + dataSessionr.sign('507f191e810c19729de860ea',{ttl:'15 minutes',dataToken:{world:'hello'},dataSession:{hello:'world'}}).then...
// Basicr.verify(token).then(decode=>{/*{ "rjwt": "507f191e810c19729de860ea:ZYYlwOGqTmx", "dataToken": [Object] "iat": 1504334208, "id": "507f191e810c19729de860ea", "ttl": 60}*/}).catch(err=>{// Wrong token})// Get data from redisr.verify(token,true).then(decode=>{/*{ "rjwt": "507f191e810c19729de860ea:ZYYlwOGqTmx", "dataToken": [Object] "dataSession": [Object] ----> get data session "iat": 1504334208, "id": "507f191e810c19729de860ea", "ttl": 60}*/}).catch(err=>{// Wrong token})
// Execute Redis comandsconstexec=r.exec();exec.rawCall(['keys',`507f191e810c19729de860ea:*`],(err,result)=>{/* [ "507f191e810c19729de860ea:ZYYlwOGqTmx", "507f191e810c19729de860ea:d39K8J249Hd", ]*/});
// Method's redis-jwtconstcall=r.call();// Test Pingcall.ping().then..// Createcall.create(key,value,ttl).then..// exits by keycall.exists(key).then..// Get ttl by Keycall.ttl(key).then..// Get values by keycall.getValueByKey(key).then..// Get values by Patterncall.getValuesByPattern(pattern).then..// Get count by Patterncall.getCountByPattern(pattern).then..// Get infocall.getInfo(section).then..// Destroy by keycall.destroy(key).then..// Destroy multiple by keycall.destroyMultiple(key).then..
// Readyr.on('ready',()=>{console.log('redis-jwt-> ready!');});// connectedr.on('connected',()=>{console.log('redis-jwt-> connected!');});// disconnectedr.on('disconnected',()=>{console.log('redis-jwt-> disconnected!');});// errorr.on('error',(err)=>{console.log('redis-jwt-> error!',err);});
npm start
npm run compile
npm test
MIT ©Leonardo Rico
About
Management of sessions by Redis and JWT for horizontal scalability, with the possibility of having one session at a time or multiple for the same user
Topics
Resources
License
Stars
Watchers
Forks
Packages0
No packages published