-
Notifications
You must be signed in to change notification settings - Fork 11
/
routes.js
52 lines (43 loc) · 1.26 KB
/
routes.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
44
45
46
47
48
49
50
51
52
module.exports = function () {
var models = require('./models'),
passport = require('passport');
function _login(req, res) {
if (req.session.user)
return res.json({error: "You'r logged in!"});
if (!req.body || !req.body.email || !req.body.password)
return res.status(400).json({error: "Bad request"});
models.User.authenticate(req.body.email, req.body.password, function (err, user) {
if ('number' === typeof err)
return res.status(err).json({error: user});
if (!user || err)
return res.status(400).json({error: err});
req.session.user = {
id: user._id,
name: user.name,
email: user.email
};
res.json(req.session.user);
});
};
function _logout(req, res) {
req.session.destroy(function (err) {
if (err)
return res.status(500).json({error: "Internal service error."});
delete req.session;
res.json({success: "Good bye!"});
});
};
function _info() {
return [
passport.authenticate('bearer', { session: false }),
function (req, res) {
res.json({ user_id: req.user.id, name: req.user.name, scope: req.authInfo.scope })
}
];
};
return {
login: _login,
logout: _logout,
info: _info
};
}