-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
jwt.js
71 lines (66 loc) 路 1.21 KB
/
jwt.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
'use strict'
const Hemera = require('./../../packages/hemera')
const nats = require('nats').connect()
const hemeraJwt = require('./../../packages/hemera-jwt-auth')
// encoded token with { scope: ['math'] }
const jwtToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6WyJtYXRoIl0sImlhdCI6MTQ4ODEyMjIwN30.UPLLbjDgkB_ajQjI7BUlpUGfZYvsqHP3NqWQIavibeQ'
const hemera = new Hemera(nats, {
logLevel: 'info',
childLogger: true
})
hemera.use(hemeraJwt, {
jwt: {
secret: 'test'
}
})
hemera.ready(() => {
hemera.add(
{
topic: 'math',
cmd: 'sub',
auth$: {
scope: 'math'
}
},
function(req, cb) {
cb(null, req.a - req.b)
}
)
hemera.add(
{
topic: 'math',
cmd: 'add',
auth$: {
scope: 'math'
}
},
function(req, cb) {
this.act(
{
topic: 'math',
cmd: 'sub',
a: req.a + req.b,
b: 100
},
function(err, res) {
cb(err, res)
}
)
}
)
hemera.act(
{
meta$: {
jwtToken
},
topic: 'math',
cmd: 'add',
a: 100,
b: 200
},
function(err, resp) {
console.log(resp)
}
)
})