-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
29 lines (24 loc) · 806 Bytes
/
middleware.js
File metadata and controls
29 lines (24 loc) · 806 Bytes
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
var cryptojs = require('crypto-js');
module.exports = function (db) {
return {
requireAuthentication: function (req, res, next) {
var token = req.get('Auth') || '';
db.token.findOne({
where: {
tokenHash: cryptojs.MD5(token).toString()
}
}).then(function (tokenInstance) {
if (!tokenInstance) {
throw new Error();
}
req.token = tokenInstance;
return db.user.findByToken(token);
}).then(function (user) {
req.user = user;
next();
}).catch(function () {
res.status(401).send();
});
}
}
};