-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
74 lines (54 loc) · 1.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import RedisJwt from './dist/index';
import express from 'express';
const r = new RedisJwt({
//host: '/tmp/redis.sock', //unix domain
host: '127.0.0.1', //can be IP or hostname
port: 6379, // port
maxretries: 10, //reconnect retries, default 10
//auth: '123', //optional password, if needed
db: 0, //optional db selection
secret: 'secret_key', // secret key for Tokens!
multiple: false, // single or multiple sessions by user
kea: true // Enable notify-keyspace-events KEA
});
const app = express();
// Events
r.on('ready', () => {
console.log('redis-jwt-> ready!');
});
r.on('connected', () => {
console.log('redis-jwt-> connected!');
});
r.on('disconnected', () => {
console.log('redis-jwt-> disconnected!');
});
r.on('error', (err) => {
console.log('redis-jwt-> error!', err);
});
// Router
app.get('/', (req, res) => {
// sign
r.sign('507f191e810c19729de860ea', {
ttl: '15 minutes',
dataToken: { hello: 'world' },
dataSession: {
hello: 'world',
headers: req.headers
}
}).then(sign => {
// verify
r.verify(sign, true).then(decode => {
// exec
var rexec = r.exec();
rexec.rawCall(['keys', `507f191e810c19729de860ea:*`], (err, exec) => {
// call
var rcall = r.call();
rcall.getValuesByPattern('507f191e810c19729de860ea').then(call => {
console.log({ sign, decode, exec, call });
res.json({ sign, decode, exec, call });
})
});
}).catch(err => console.log('error verify-> ', err));
});
});
app.listen(3000, () => console.log('Server listening on port 3000!'));