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
|
const User = require('../models/user');
const bcrypt = require('bcryptjs');
module.exports = {
register(req, res) {
if (req.body.password === req.body.confirmPassword) {
const newUser = new User({
firstname: req.body.firstname,
lastname: req.body.lastname,
email: req.body.email,
password: bcrypt.hashSync(req.body.password)
});
newUser.save()
.then(() => {
req.login(newUser, () => res.redirect('/new-post'));
})
.catch(err => {
console.log(err);
req.flash('msg', 'Validation errors...');
res.redirect('/register');
});
}
else {
req.flash('msg', 'Validation errors...');
res.redirect('/register');
}
},
logout(req, res) {
req.logout();
res.redirect('/');
},
registerView(req, res) {
res.render('register', {
title: 'Register',
auth: req.isAuthenticated(),
register: true,
msg: req.flash('msg')
});
},
loginView(req, res) {
res.render('login', {
title: 'Login',
auth: req.isAuthenticated(),
login: true,
msg: req.flash('msg')
});
}
};
|