diff --git a/public/templates/reset_code.tpl b/public/templates/reset_code.tpl index 8d98b8e0a6..90115baaa0 100644 --- a/public/templates/reset_code.tpl +++ b/public/templates/reset_code.tpl @@ -3,7 +3,7 @@ @@ -30,11 +29,11 @@ resetEl.addEventListener('click', function() { if (password.value.length < 6) { $('#error').hide(); - noticeEl.querySelector('strong').value = 'Invalid Password'; - noticeEl.querySelector('p').value = 'The password entered it too short, please pick a different password!'; - noticeEl.style.display = 'auto'; + noticeEl.querySelector('strong').innerHTML = 'Invalid Password'; + noticeEl.querySelector('p').innerHTML = 'The password entered it too short, please pick a different password!'; + noticeEl.style.display = 'block'; } else if (password.value === repeat.value) { - alert("match"); + socket.emit('user:reset.commit', { code: '{reset_code}', password: password.value }); } }, false); @@ -50,10 +49,12 @@ } }) - // socket.on('user.password.reset', function(data) { - // if (data.success === 'ok') { - // ajaxify.go('/'); - // } - // }); + socket.on('user:reset.commit', function(data) { + if (data.status === 'ok') { + $('#error').hide(); + $('#notice').hide(); + $('#success').show(); + } + }); }()); \ No newline at end of file diff --git a/src/user.js b/src/user.js index 3e07fe92ed..2c621e1582 100644 --- a/src/user.js +++ b/src/user.js @@ -110,19 +110,27 @@ var config = require('../config.js'), }; User.reset = { - validate: function(code) { + validate: function(code, callback) { + if (typeof callback !== 'function') callback = undefined; + RDB.get('reset:' + code + ':uid', function(uid) { if (uid !== null) { RDB.get('reset:' + code + ':expiry', function(expiry) { - if (expiry >= +new Date()/1000|0) global.socket.emit('user:reset.valid', { valid: true }); - else { + if (expiry >= +new Date()/1000|0) { + if (!callback) global.socket.emit('user:reset.valid', { valid: true }); + else callback(true); + } else { // Expired, delete from db RDB.del('reset:' + code + ':uid'); RDB.del('reset:' + code + ':expiry'); - global.socket.emit('user:reset.valid', { valid: false }); + if (!callback) global.socket.emit('user:reset.valid', { valid: false }); + else callback(false); } }); - } else global.socket.emit('user:reset.valid', { valid: false }); + } else { + if (!callback) global.socket.emit('user:reset.valid', { valid: false }); + else callback(false); + } }); }, send: function(email) { @@ -173,6 +181,19 @@ var config = require('../config.js'), }); } }); + }, + commit: function(code, password) { + this.validate(code, function(validated) { + if (validated) { + RDB.get('reset:' + code + ':uid', function(uid) { + RDB.set('uid:' + uid + ':password', password); + RDB.del('reset:' + code + ':uid'); + RDB.del('reset:' + code + ':expiry'); + + global.socket.emit('user:reset.commit', { status: 'ok' }); + }); + } + }); } } diff --git a/src/websockets.js b/src/websockets.js index fb7f2e5ee4..d88a4cdff7 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -52,6 +52,10 @@ var SocketIO = require('socket.io').listen(global.server); socket.on('user:reset.valid', function(data) { modules.user.reset.validate(data.code); }); + + socket.on('user:reset.commit', function(data) { + modules.user.reset.commit(data.code, data.password); + }); }); }(SocketIO));