-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublic.js
97 lines (84 loc) · 2.73 KB
/
public.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var express = require('express');
var router = express.Router();
var jwt = require('jsonwebtoken');
var bcrypt = require('bcrypt');
var jwtSecret = require('../../config/jwt').secret;
// Models
var User = require('../models/User');
router.post('/authenticate', function(req, res) {
User.findOne({
name: req.body.username
}, function(err, user) {
if (err) {
if (err.name === 'TokenExpiredError') {
res.status(401).json({
success: false,
message: 'Token expired.'
})
return;
} else if (err.name === 'JsonWebTokenError') {
res.status(401).json({
success: false,
message: err.message
})
return;
} else {
res.status(401).json({
success: false,
message: 'Unspecified error.'
})
return console.error(err);
}
}
if (!user) {
res.status(401).json({
success: false,
message: 'Authentication failed. User not found.'
});
return;
}
user.comparePassword(req.body.password, function(err, isMatch) {
if (err) {
return res.status(401).json({
success: false,
message: err
});
}
if (!isMatch) {
return res.status(401).json({
success: false,
message: 'Authentication failed. Wrong password'
})
}
// if everything is good, create a JWT!
var token = jwt.sign(user.clean(), jwtSecret, {
// if you're signing on from a mobile app, we don't want tokens
// to expire, so let's set 'expiresInMinutes' to 0.
expiresInMinutes: (req.body.ios) ? 0 : 1440 // expires in 24 hours
});
// return the info as JSON
res.json({
success: true,
message: 'Enjoy your token!',
token: token
});
});
});
});
router.get('/setup', function(req, res) {
// uncomment this if you want this to
// be accessible to the public.
return res.json({ success: false });
// this is an easy way to create your
// initial admin user in your database
var parker = new User({
name: 'parker',
password: 'hi',
admin: true
});
parker.save(function(err) {
if (err) throw err;
res.json({ success: true });
});
});
module.exports = router;