-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
55 lines (40 loc) · 1.38 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
var passport = require('passport');
var Account = require('./models/account');
var routes = require('./routes/index');
module.exports = function (app) {
app.get('/', function (req, res) {
res.render('index', { user: req.user });
});
app.get('/register', function (req, res) {
res.render('register', {});
});
app.post('/register', function (req, res) {
Account.register(new Account({ username: req.body.username }), req.body.password, function (err, account) {
if (err) {
return res.render('register', { account: account });
}
passport.authenticate('local')(req, res, function () {
res.redirect('/');
});
});
});
app.get('/login', function (req, res) {
res.render('login', { user: req.user });
});
app.post('/login', passport.authenticate('local'), function (req, res) {
res.redirect('/');
});
app.get('/logout', function (req, res) {
req.logout();
res.redirect('/');
});
app.get('/ping', function (req, res) {
res.send("pong!", 200);
});
var isAuthenticated = function (req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/');
}
app.use('/api', isAuthenticated, routes);
};