-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
80 lines (79 loc) · 2.7 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
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
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var Account = require('./models/account');
var Admin = require('./models/admin');
passport.use(new LocalStrategy(Admin.authenticate()));
module.exports = function(app) {
app.get('/', function(req, res) {
res.render('index');
});
app.route('/register')
.get(function(req, res) {
res.render('register');
})
.post(function(req, res) {
var password = req.body.password;
req.body.pId = password;
delete req.body.password;
Account.register(new Account(req.body), password, function(err, account) {
if (err) {
console.log(new Date() + ' ' + err);
return res.json({msg:err});
} else {
console.log('學員' + account.username + ' 已於 ' + new Date() + ' 報名');
}
return res.json({msg:"success"});
});
});
app.route('/check')
.post(function(req, res) {
Account.findByUsername(req.body.email, function(err, account) {
if (!account) {
res.json({
msg: "notExisted"
});
} else {
account.authenticate(req.body.password, function(err, account) {
if (account) res.json({
msg: "existed"
});
else res.json("notExisted");
})
}
})
});
app.route('/list')
.get(function(req, res) {
if (req.user) {
var accounts = {};
Account.find({}, function(err, users) {
users.forEach(function(user) {
console.log(user);
accounts[user._id.toString()] = user;
})
return res.json(accounts);
});
} else {
res.status(401).redirect('/');
}
})
.post(passport.authenticate('local', {
failureRedirect: '/'
}), function(req, res) {
res.json({
msg: "adminLoggedIn"
});
});
app.post('/adminReg', function(req, res) {
Admin.register(new Admin({
username: req.body.username
}), req.body.password, function(err, admin) {
if (err) res.send(err);
else res.send("An admin has been created");
})
});
app.get('/adminLogout', function(req, res) {
req.logout();
res.redirect('/');
});
};