-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
74 lines (62 loc) · 1.97 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
var passport = require('passport'),
UserModel = require('./model/user.js'),
api = require("./api.js");
module.exports = function (app) {
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login');
}
app.get('/', ensureAuthenticated, function (req, res) {
res.cookie("userName", req.user.username);
res.cookie("userId", req.user._id);
res.render('index', {
user: req.user
});
});
app.get('/register', function(req, res) {
res.render('register', {});
});
app.post('/register', function(req, res) {
req.assert('username', 'Username is required').notEmpty();
req.assert('password', 'Password is too short').len(4, 20);
req.assert('password', 'Passwords do not match').equals(req.body.password_confirmation);
var errors = req.validationErrors();
if (!errors){
UserModel.register(new UserModel({ username : req.body.username }), req.body.password, function(err, account) {
if (err) {
return res.render('register', { user : account });
}
res.redirect('/');
});
} else {
res.render('register', {
message: '',
errors: errors
});
}
});
app.get('/login', function(req, res) {
res.render('login', { message: req.flash('error') });
});
app.post('/login', passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login',
failureFlash: true // 'Invalid username or password.'
})
);
app.get('/logout', function(req, res) {
res.clearCookie('userName');
res.clearCookie('userId');
req.logout();
res.redirect('/');
});
// API
app.get('/chat', function (req, res) {
res.send('Chat is running');
});
app.get('/chat/message', api.getMessages);
app.post('/chat/message', api.postMessage);
// app.get('/api/notes/:id', api.getNote);
// app.put('/api/notes/:id', api.updateNote);
// app.delete('/api/notes/:id', api.deleteNote);
};