redis-jwt
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
import RedisJWT from 'redis-jwt';
const r = new RedisJWT({
host: '127.0.0.1',
port: 6379,
maxretries: 10,
db: 0,
secret: 'secret_key',
multiple: false,
KEA: true
});
import express from 'express';
const app = express();
app.get('/login', (req, res) => {
r.create(req, 'id_user').then(token => {
res.json({token});
});
});
app.get('/me', mw(), (req, res) => {
res.json(req.user);
});
function mw() {
return (req, res, next) => {
const token = req.headers['authorization'];
r.verify(token).then(session => {
req.user = {name: 'led', lastname: 'zeppelin' ,{session}};
next();
}).catch(err => {
res.status(401).json({err})
})
}
}
app.listen(3000, () => {
console.log('Server listening on port 3000!');
});
Options
Create
r.create(req, 'id_user').then(token => {
res.json({token});
});
r.create(req, 'id_user',{hello:'world'}).then(token => {
res.json({token});
});
r.create(req, 'id_user', 5000).then(token => {
res.json({token});
});
r.create(req, 'id_user', {hello:'world'}, 5000).then(token => {
res.json({token});
});
Verify
r.verify(token).then(result => {
}).catch(err => {
})
Exec
var exec = r.exec();
exec.rawCall(['keys', `507f191e810c19729de860ea:*`], (err, result) => {
console.log(result);
});
Call
var call = r.call();
call.getValuesByPattern('507f191e810c19729de860ea').then(result => {
console.log(result);
})
call.ping()..
call.create(key, value, ttl)..
call.exists(key)..
call.ttl(key)..
call.getValueByKey(key)..
call.getValuesByPattern(pattern)..
call.getCountByPattern(pattern)..
call.getInfo(section)..
call.destroy(key)..
call.destroyMultiple(key)..
Development
Start
npm start
Serve
npm run serve
Build
npm run build
Test
npm test
License
MIT © Leonardo Rico