-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
42 lines (38 loc) · 1.37 KB
/
auth.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
var UserModel = require('./app/models/user');
exports.register = function (server, options, next) {
server.auth.strategy('jwt', 'jwt', false,
{
key: options.key,
verifyOptions: options.verifyOptions,
validateFunc: function (decoded, request, callback) {
if (!decoded['sub'] || decoded['scope'].indexOf('refresh') !== -1) {
return callback(null, false);
} else {
return callback(null, true);
}
}
});
server.auth.strategy('jwt-refresh', 'jwt', false,
{
key: options.key,
verifyOptions: options.verifyOptions,
validateFunc: function (decoded, request, callback) {
if (!decoded['sub']) {
return callback(null, false);
} else {
var User = UserModel.User;
User.findOne({ _id: decoded.sub, active: true}, function(err, user) {
if (err || !user || user.jti !== decoded.jti) {
return callback(null, false);
}
return callback(null, true);
});
}
}
});
// server.auth.default('jwt');
next();
};
exports.register.attributes = {
name: 'auth'
};