forked from fmbenhassine/gamehub.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.js
48 lines (44 loc) · 1.71 KB
/
account.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
var express = require('express');
var mongoose = require('mongoose');
var util = require('../config/util.js');
var router = express.Router();
var moment = require('moment');
/* GET user account details. */
router.get('/', function(req, res) {
res.render('partials/account', {
title: 'Chess Hub - Account',
user: req.user,
isAccountPage: true,
lastConnection: moment(req.user.lastConnection).fromNow(),
updateStatus: req.flash('updateStatus'),
updateMessage: req.flash('updateMessage')
});
});
/* Update user account. */
router.post('/', function(req, res) {
var User = mongoose.model('User');
var currentPassword = req.body.password;
var newPassword = req.body.newPassword;
var confirmNewPassword = req.body.confirmNewPassword;
var hash = util.encrypt(currentPassword);
if ( hash === req.user.password ) {
if ( newPassword === confirmNewPassword ) {
var newPasswordHash = util.encrypt(newPassword);
User.findOneAndUpdate({_id: req.user._id}, { password: newPasswordHash }, {} ,function (err, user) {
req.user = user;
req.flash('updateStatus', true);
req.flash('updateMessage', 'Your password has been updated successfully');
res.redirect('/account');
});
} else {
req.flash('updateStatus', false);
req.flash('updateMessage', 'The confirmation password does not match the new password');
res.redirect('/account');
}
} else {
req.flash('updateStatus', false);
req.flash('updateMessage', 'The current password is incorrect');
res.redirect('/account');
}
});
module.exports = router;