-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (37 loc) · 1.12 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
const jwt = require('jsonwebtoken');
const scErrors = require('sc-errors');
const InvalidArgumentsError = scErrors.InvalidArgumentsError;
let AuthEngine = function () {};
AuthEngine.prototype.verifyToken = function (signedToken, key, options) {
options = options || {};
let jwtOptions = Object.assign({}, options);
delete jwtOptions.socket;
if (typeof signedToken === 'string' || signedToken == null) {
return new Promise((resolve, reject) => {
jwt.verify(signedToken || '', key, jwtOptions, (err, token) => {
if (err) {
reject(err);
return;
}
resolve(token);
});
});
}
return Promise.reject(
new InvalidArgumentsError('Invalid token format - Token must be a string')
);
};
AuthEngine.prototype.signToken = function (token, key, options) {
options = options || {};
let jwtOptions = Object.assign({}, options);
return new Promise((resolve, reject) => {
jwt.sign(token, key, jwtOptions, (err, signedToken) => {
if (err) {
reject(err);
return;
}
resolve(signedToken);
});
});
};
module.exports = AuthEngine;