Node.js based forum software built for the modern web
password change, closes #66
Baris Usakli1d39b08195fc
diff --git a/public/css/style.less b/public/css/style.lessindex 2b7f64c5d6..b24337c26b 100644--- a/public/css/style.less+++ b/public/css/style.less@@ -204,7 +204,6 @@ footer.footer {.user-profile-picture {width:128px;- height:128px;margin-bottom:10px;}diff --git a/public/src/forum/accountedit.js b/public/src/forum/accountedit.jsindex f4f97b2629..5899ac81b9 100644--- a/public/src/forum/accountedit.js+++ b/public/src/forum/accountedit.js@@ -219,5 +219,66 @@ $(document).ready(function() {$('#uploadForm').submit();});-+ (function handlePasswordChange() {+ var currentPassword = $('#inputCurrentPassword');+ var password_notify = $('#password-notify');+ var password_confirm_notify = $('#password-confirm-notify');+ var password = $('#inputNewPassword');+ var password_confirm = $('#inputNewPasswordAgain');+ var passwordvalid = false;+ var passwordsmatch = false;+++ function onPasswordChanged() {+ passwordvalid = utils.isPasswordValid(password.val());+ if (password.val().length < 6) {+ password_notify.html('Password too short');+ password_notify.attr('class', 'label label-important');+ } else if(!passwordvalid) {+ password_notify.html('Invalid password');+ password_notify.attr('class', 'label label-important');+ } else {+ password_notify.html('OK!');+ password_notify.attr('class', 'label label-success');+ }++ onPasswordConfirmChanged();+ }++ function onPasswordConfirmChanged() {+ if(password.val() !== password_confirm.val()) {+ password_confirm_notify.html('Passwords must match!');+ password_confirm_notify.attr('class', 'label label-important');+ passwordsmatch = false;+ } else {+ password_confirm_notify.html('OK!');+ password_confirm_notify.attr('class', 'label label-success');+ passwordsmatch = true;+ }+ }++ password.on('keyup', onPasswordChanged);+ password_confirm.on('keyup', onPasswordConfirmChanged);++ $('#changePasswordBtn').on('click', function() {+ if(passwordvalid && passwordsmatch && currentPassword.val()) {+ socket.emit('api:user.changePassword', {+ 'currentPassword': currentPassword.val(),+ 'newPassword': password.val()+ });+ }+ return false;+ });++ socket.on('api:user.changePassword', function(data) {+ currentPassword.val('');+ password.val('');+ password_confirm.val('');+ password_notify.html('');+ password_confirm_notify.html('');+ passwordsmatch = false;+ passwordvalid = false;+ });++ }());});\ No newline at end of filediff --git a/public/templates/accountedit.tpl b/public/templates/accountedit.tplindex 7f39a8cd3b..e96b9b6d44 100644--- a/public/templates/accountedit.tpl+++ b/public/templates/accountedit.tpl@@ -80,7 +80,7 @@</div></div>- <div class="span5">+ <div class="span10"><div class="inline-block"><form class='form-horizontal'><div class="control-group">@@ -133,38 +133,37 @@</form></div>- </div>- <div class="span5">- <div class="inline-block">+ <div class="inline-block" style="vertical-align:top;"><form class='form-horizontal'><div class="control-group"><label class="control-label" for="inputCurrentPassword">Current Password</label><div class="controls">- <input type="text" id="inputCurrentPassword" placeholder="Current Password" value="">+ <input type="password" id="inputCurrentPassword" placeholder="Current Password" value=""></div></div><div class="control-group"><label class="control-label" for="inputNewPassword">Password</label><div class="controls">- <input type="text" id="inputNewPassword" placeholder="New Password" value="">+ <input type="password" id="inputNewPassword" placeholder="New Password" value=""><br/><span id="password-notify" class="label label-important"></span></div></div><div class="control-group"><label class="control-label" for="inputNewPasswordAgain">Confirm Password</label><div class="controls">- <input type="text" id="inputNewPasswordAgain" placeholder="Confirm Password" value="">+ <input type="password" id="inputNewPasswordAgain" placeholder="Confirm Password" value=""><br/><span id="password-confirm-notify" class="label label-important"></span></div></div>-+<div class="form-actions"><a id="changePasswordBtn" href="#" class="btn btn-primary">Change Password</a></div>-+</form></div></div>+</div></div>diff --git a/src/login.js b/src/login.jsindex 56be3a7b2c..6d676bdb29 100644--- a/src/login.js+++ b/src/login.js@@ -34,7 +34,7 @@ var user = require('./user.js'),return;}- if (res === true) {+ if (res) {next({status: "ok",user: {diff --git a/src/user.js b/src/user.jsindex 2f7bdec5f4..b5a488ef15 100644--- a/src/user.js+++ b/src/user.js@@ -201,6 +201,51 @@ var utils = require('./../public/src/utils.js'),callback({});}+ User.changePassword = function(socket, uid, data, callback) {+ if(!utils.isPasswordValid(data.newPassword)) {+ socket.emit('event:alert', {+ title: 'Error',+ message: 'Invalid password!',+ type: 'error',+ timeout: 2000+ });+ callback(false);+ return;+ }++ User.getUserField(uid, 'password', function(user_password) {+ bcrypt.compare(data.currentPassword, user_password, function(err, res) {+ if(err) {+ console.log(err);+ callback(false);+ return;+ }++ if (res) {+ User.hashPassword(data.newPassword, function(hash) {+ User.setUserField(uid, 'password', hash);++ socket.emit('event:alert', {+ title: 'Success',+ message: 'Your password is updated!',+ type: 'success',+ timeout: 2000+ });+ callback(true);+ });+ } else {+ socket.emit('event:alert', {+ title: 'Warning',+ message: 'Your current password is not correct!',+ type: 'warning',+ timeout: 2000+ });+ callback(false);+ }+ });+ });+ }+User.setUserField = function(uid, field, value) {RDB.hset('user:' + uid, field, value);}diff --git a/src/websockets.js b/src/websockets.jsindex 757bf2c9e3..981b8e77c0 100644--- a/src/websockets.js+++ b/src/websockets.js@@ -190,6 +190,14 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),user.get_online_users(socket, data);});+ socket.on('api:user.changePassword', function(data) {+ user.changePassword(socket, uid, data, function(success) {+ if(success) {+ socket.emit('api:user.changePassword');+ }+ });+ });+socket.on('api:topics.post', function(data) {topics.post(socket, uid, data.title, data.content, data.category_id);});