-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (27 loc) · 884 Bytes
/
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
'use strict';
var restify = require('restify'),
jwt = require('jsonwebtoken'),
path = require('path');
module.exports = function(options) {
if (!options || !options.secret) {
throw new Error('Secret should be set');
}
return function(req, res, next) {
if (req.headers && req.headers.authorization) {
var test = req.headers.authorization.match(/^Bearer (.*)$/);
if (test) {
jwt.verify(test[1], options.secret, options, function(err, user) {
if (err) {
return next(new restify.NotAuthorizedError('Invalid token'));
}
req.user = user;
next();
});
} else {
return next(new restify.NotAuthorizedError('Format is Authorization: Bearer [token]'));
}
} else {
return next(new restify.NotAuthorizedError('No authorization header was found'));
}
};
};