Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM versionBuild Statusdependencies StatusdevDependencies StatusGitHub license

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

Requirements

  • Nodejs>= 6.x.x (Recommended 8.x.x)
  • Redis>= 3.x.x (Recommended 4.x.x)

Installation

Npm

npm install redis-jwt --save

Yarn

yarn add redis-jwt

Usage

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});});

Example Redis-jwt with Express

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!'));

Options

Sign

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

Verify

// 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})

Exec

// Execute Redis comandsconstexec=r.exec();exec.rawCall(['keys',`507f191e810c19729de860ea:*`],(err,result)=>{/* [  "507f191e810c19729de860ea:ZYYlwOGqTmx",  "507f191e810c19729de860ea:d39K8J249Hd", ]*/});

Call

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

Events

// 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);});

Development

Start

npm start

Compile

npm run compile

Test

npm test

License

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

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp