From 24944762d7ff68c3eaf1e2fa74fd1e2aa67adb5d Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 2 Jan 2014 16:26:56 -0500 Subject: [PATCH] fixing 'back to nodebb' ext, and revamping email confirm logic --- public/language/en/notifications.json | 2 +- public/language/es/notifications.json | 2 +- public/language/fr/notifications.json | 2 +- public/templates/confirm.tpl | 2 +- src/routes/api.js | 2 +- src/user.js | 107 ++++++++++---------------- 6 files changed, 45 insertions(+), 72 deletions(-) diff --git a/public/language/en/notifications.json b/public/language/en/notifications.json index bfe2a48ab8..a115e1d6a9 100644 --- a/public/language/en/notifications.json +++ b/public/language/en/notifications.json @@ -1,6 +1,6 @@ { "title": "Notifications", - "back_to_home": "back to NodeBB", + "back_to_home": "Back to NodeBB", "mark_all_as_read": "Mark All as Read", "outgoing_link": "Outgoing Link", "outgoing_link_message": "You are now leaving", diff --git a/public/language/es/notifications.json b/public/language/es/notifications.json index cc15c86859..3ff280199b 100644 --- a/public/language/es/notifications.json +++ b/public/language/es/notifications.json @@ -1,6 +1,6 @@ { "title": "Notificaciones", - "back_to_home": "volver al Inicio", + "back_to_home": "Volver al Inicio", "mark_all_as_read": "Marcar todo como leeido", "outgoing_link": "Link Externo", "outgoing_link_message": "Estas saliendo del sitio", diff --git a/public/language/fr/notifications.json b/public/language/fr/notifications.json index 7a4ef3e3c3..844fbf2b9f 100644 --- a/public/language/fr/notifications.json +++ b/public/language/fr/notifications.json @@ -1,6 +1,6 @@ { "title": "Notifications", - "back_to_home": "retour à NodeBB", + "back_to_home": "Retour à NodeBB", "mark_all_as_read": "Tout marquer comme lu", "outgoing_link": "Lien Sortant", "outgoing_link_message": "Vous quitter NodeBB", diff --git a/public/templates/confirm.tpl b/public/templates/confirm.tpl index 255c59c005..ef9638a906 100644 --- a/public/templates/confirm.tpl +++ b/public/templates/confirm.tpl @@ -2,6 +2,6 @@ {title}

{text}

- [[notification:back_to_home]] + [[notifications:back_to_home]]

diff --git a/src/routes/api.js b/src/routes/api.js index c637dd58f5..7aa336961f 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -221,7 +221,7 @@ var path = require('path'), }); } else { res.json({ - 'alert-class': 'alert-error', + 'alert-class': 'alert-danger', title: 'An error occurred...', text: 'There was a problem validating your email address. Perhaps the code was invalid or has expired.' }); diff --git a/src/user.js b/src/user.js index 33af2557a5..efa3fadb32 100644 --- a/src/user.js +++ b/src/user.js @@ -103,7 +103,7 @@ var bcrypt = require('bcrypt'), if (email !== undefined) { db.setObjectField('email:uid', email, uid); - User.sendConfirmationEmail(uid, email); + User.email.verify(uid, email); } plugins.fireHook('action:user.create', {uid: uid, username: username, email: email, picture: gravatar, timestamp: timestamp}); @@ -823,45 +823,6 @@ var bcrypt = require('bcrypt'), } }; - User.sendConfirmationEmail = function(uid, email) { - var confirm_code = utils.generateUUID(), - confirm_link = nconf.get('url') + 'confirm/' + confirm_code; - - // Email confirmation code - var expiry_time = Date.now() / 1000 + 60 * 60 * 2; - - db.setObjectField('email:confirm', email, confirm_code); - - db.setObjectField('confirm:email', confirm_code, email); - db.setObjectField('confirm:email', confirm_code + ':expire', expiry_time); - - // Send intro email w/ confirm code - User.getUserField(uid, 'username', function(err, username) { - Emailer.send('welcome', uid, { - 'site_title': (meta.config['title'] || 'NodeBB'), - subject: 'Welcome to ' + (meta.config['title'] || 'NodeBB') + '!', - username: username, - 'confirm_link': confirm_link - }); - }); - // var message = emailjs.message.create({ - // text: confirm_email_plaintext, - // from: meta.config['email:from'] || 'localhost@example.org', - // to: email, - // subject: '[NodeBB] Registration Email Verification', - // attachment: [{ - // data: confirm_email, - // alternative: true - // }] - // }); - - // emailjsServer.send(message, function(err, success) { - // if (err) { - // console.log(err); - // } - // }); - }; - User.pushNotifCount = function(uid) { User.notifications.getUnreadCount(uid, function(err, count) { if (!err) { @@ -873,6 +834,32 @@ var bcrypt = require('bcrypt'), }; User.email = { + verify: function(uid, email) { + var confirm_code = utils.generateUUID(), + confirm_link = nconf.get('url') + 'confirm/' + confirm_code; + + async.series([ + function(next) { + db.setObject('confirm:' + confirm_code, { + email: email, + uid: uid + }, next); + }, + function(next) { + db.expireAt('confirm:' + confirm_code, Math.floor(Date.now() / 1000 + 60 * 60 * 2), next); + } + ], function(err) { + // Send intro email w/ confirm code + User.getUserField(uid, 'username', function(err, username) { + Emailer.send('welcome', uid, { + 'site_title': (meta.config['title'] || 'NodeBB'), + subject: 'Welcome to ' + (meta.config['title'] || 'NodeBB') + '!', + username: username, + 'confirm_link': confirm_link + }); + }); + }); + }, exists: function(socket, email, callback) { User.getUidByEmail(email, function(err, exists) { exists = !! exists; @@ -886,37 +873,23 @@ var bcrypt = require('bcrypt'), }); }, confirm: function(code, callback) { - db.getObjectFields('confirm:email', [code, code + ':expire'], function(err, data) { + db.getObject('confirm:' + code, function(err, confirmObj) { if (err) { - return callback({ - status:'error' - }); - } - - var email = data.email; - var expiry = data[code + ':expire']; - if (parseInt(expiry, 10) >= Date.now() / 1000) { - - db.deleteObjectField('confirm:email', code); - db.deleteObjectField('confirm:email', code + ':expire'); - - return callback({ - status: 'expired' - }); - } - - if (email !== null) { - db.setObjectField('email:confirm', email, true); - - db.deleteObjectField('confirm:email', code); - db.deleteObjectField('confirm:email', code + ':expire'); callback({ - status: 'ok' + status:'error' }); } else { - callback({ - status: 'not_ok' - }); + if (confirmObj.uid && confirmObj.email) { + db.setObjectField('email:confirmed', confirmObj.email, '1', function() { + callback({ + status: 'ok' + }); + }); + } else { + callback({ + status: 'not_ok' + }); + } } }); }