-
Notifications
You must be signed in to change notification settings - Fork 3
/
loginapi.js
70 lines (60 loc) · 1.43 KB
/
loginapi.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//api for user authentification.
var express = require('express');
var router = express.Router();
var passport = require('passport');
var User = require('./app/models/user.js');
router.post('/register', function(req, res) {
User.register(new User({ username: req.body.username }),
req.body.password, function(err, account) {
if (err) {
return res.status(500).json({
err: err
});
}
passport.authenticate('local')(req, res, function () {
return res.status(200).json({
status: 'Registration successful!'
});
});
});
});
router.post('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) {
return next(err);
}
if (!user) {
return res.status(401).json({
err: info
});
}
req.login(user, function(err) {
if (err) {
return res.status(500).json({
err: 'Could not log in user'
});
}
res.status(200).json({
status: 'Login successful!'
});
/* req.session.user=user;*/
});
})(req, res, next);
});
router.get('/logout', function(req, res) {
req.logout();
res.status(200).json({
status: 'Bye!'
});
});
router.get('/status', function(req, res) {
if (!req.isAuthenticated()) {
return res.status(200).json({
status: false
});
}
res.status(200).json({
status: true
});
});
module.exports = router;