home.js
1.13 KB
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
var express = require('express');
var router = express.Router();
var passport = require('../config/passport');
// Home
router.get('/', function(req, res){
res.render('home/welcome');
});
router.get('/about', function(req, res){
res.render('home/about');
});
// Login
router.get('/login', function (req,res) {
var username = req.flash('username')[0];
var errors = req.flash('errors')[0] || {};
res.render('home/login', {
username:username,
errors:errors
});
});
// Post Login
router.post('/login',
function(req,res,next){
var errors = {};
var isValid = true;
if(!req.body.username){
isValid = false;
errors.username = 'Username is required!';
}
if(!req.body.password){
isValid = false;
errors.password = 'Password is required!';
}
if(isValid){
next();
}
else {
req.flash('errors',errors);
res.redirect('/login');
}
},
passport.authenticate('local-login', {
successRedirect : '/posts',
failureRedirect : '/login'
}
));
// Logout
router.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
module.exports = router;