forked from jmcooper/ng2f-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
40 lines (36 loc) · 973 Bytes
/
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
var passport = require('passport');
// super important that you use "username" in the body.
exports.authenticate = function(req, res, next) {
req.body.username = req.body.username.toLowerCase();
var auth = passport.authenticate('local', function(err, user) {
if(err) {return next(err);}
if(!user) { res.sendStatus(403); }
req.logIn(user, function(err) {
if(err) {return next(err);}
res.send({success:true, user: user});
})
})
auth(req, res, next);
};
exports.getCurrentIdentity = function(req, res, next) {
res.status(200).send(req.user);
res.end();
}
exports.requiresApiLogin = function(req, res, next) {
if(!req.isAuthenticated()) {
res.status(403);
res.end();
} else {
next();
}
};
exports.requiresRole = function(role) {
return function(req, res, next) {
if(!req.isAuthenticated() || req.user.roles.indexOf(role) === -1) {
res.status(403);
res.end();
} else {
next();
}
}
}