application.js
1.39 KB
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
'use strict';
const koajwt = require('koa-jwt2');
const UnauthorizedError = require('koa-jwt2/lib/errors/UnauthorizedError');
const jwt = require('jsonwebtoken');
const JWT = Symbol('Application#jwt');
module.exports = {
get jwt() {
if (!this[JWT]) {
const config = this.config.jwt;
this[JWT] = koajwt(config);
this[JWT].sign = (payload, secret, options, callback) => {
if (typeof secret !== 'string') {
callback = options;
options = secret || {};
secret = config.secret;
} else if (typeof options === 'function') {
callback = options;
options = {};
}
return jwt.sign(
payload,
secret,
Object.assign({}, config.sign || {}, options),
callback
);
};
this[JWT].verify = (token, secret, options, callback) => {
if (typeof secret !== 'string') {
callback = options;
options = secret || {};
secret = config.secret;
} else if (typeof options === 'function') {
callback = options;
options = {};
}
return jwt.verify(
token,
secret,
Object.assign({}, config.verify || {}, options),
callback
);
};
this[JWT].decode = jwt.decode;
this[JWT].UnauthorizedError = UnauthorizedError;
}
return this[JWT];
},
};