Node.js based forum software built for the modern web
added email verification on registration added email header and footer
Julian Lam1fc6e0db2c72
diff --git a/public/src/templates.js b/public/src/templates.jsindex 3024dac88d..8ab24d7fc9 100644--- a/public/src/templates.js+++ b/public/src/templates.js@@ -49,10 +49,11 @@ var templates = {};function init() {loadTemplates([- 'header', 'footer', 'register', 'home', 'topic',- 'login', 'reset', 'reset_code', 'account',- 'emails/reset', 'emails/reset_plaintext'- ]);+ 'header', 'footer', 'register', 'home', 'topic',+ 'login', 'reset', 'reset_code', 'account',+ 'confirm',+ 'emails/reset', 'emails/reset_plaintext', 'emails/email_confirm', 'emails/email_confirm_plaintext'+ ]);}diff --git a/public/templates/confirm.tpl b/public/templates/confirm.tplnew file mode 100644index 0000000000..41f6363398--- /dev/null+++ b/public/templates/confirm.tpl@@ -0,0 +1,7 @@+<div class="alert {alert-class}">+ <strong>{title}</strong>+ <p>{text}</p>+ <p>+ <a href="/">NodeBB Home</a>+ </p>+</div>diff --git a/public/templates/emails/email_confirm.tpl b/public/templates/emails/email_confirm.tplnew file mode 100644index 0000000000..2f3159c613--- /dev/null+++ b/public/templates/emails/email_confirm.tpl@@ -0,0 +1,7 @@++<p>Hello,</p>+<p><strong>Thank you for registering with NodeBB!</strong></p>+<p>To fully activate your account, we need to verify that you own the email address you registered with.</p>+<p>Please click on the following link:</p>+<blockquote>{CONFIRM_LINK}</blockquote>+<p>Thanks!<br /><strong>NodeBB</strong>\ No newline at end of filediff --git a/public/templates/emails/email_confirm_plaintext.tpl b/public/templates/emails/email_confirm_plaintext.tplnew file mode 100644index 0000000000..a01d7c90cc--- /dev/null+++ b/public/templates/emails/email_confirm_plaintext.tpl@@ -0,0 +1,12 @@+Hello,++Thank you for registering with NodeBB!++To fully activate your account, we need to verify that you own the email address you registered with.++Please click on the following link:++ {CONFIRM_LINK}++Thanks!+NodeBB\ No newline at end of filediff --git a/public/templates/emails/footer.tpl b/public/templates/emails/footer.tplnew file mode 100644index 0000000000..5fd5205487--- /dev/null+++ b/public/templates/emails/footer.tpl@@ -0,0 +1,10 @@++ </div>+ <hr />+ <p style="+ font-size: 10px;+ padding: 6px 12px;+ ">+ This email was sent by NodeBB. If it does not apply to you, please ignore it.+ </p>+</div>\ No newline at end of filediff --git a/public/templates/emails/header.tpl b/public/templates/emails/header.tplnew file mode 100644index 0000000000..9c3bf39b76--- /dev/null+++ b/public/templates/emails/header.tpl@@ -0,0 +1,26 @@+<div style="+ width: 640px;+ border: 1px solid #666;+ margin: 0 auto;+ font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif;+ background: #f6f6f6;+">+ <h1 style="+ background-color: #1b1b1b;+ margin: 0;+ padding: 6px 12px;+ font-size: 20px;+ background-image: -moz-linear-gradient(top,#222,#111);+ background-image: -webkit-gradient(linear,0 0,0 100%,from(#222),to(#111));+ background-image: -webkit-linear-gradient(top,#222,#111);+ background-image: -o-linear-gradient(top,#222,#111);+ background-image: linear-gradient(to bottom,#222,#111);+ color: #777;+ font-weight: normal;+ ">+ NodeBB+ </h1>+ <div style="+ padding: 20px;+ color: #333;+ ">diff --git a/src/templates.js b/src/templates.jsindex 17b41592d7..5e93378333 100644--- a/src/templates.js+++ b/src/templates.js@@ -28,7 +28,8 @@'header', 'footer', 'register', 'home', 'topic', 'account','login', 'reset', 'reset_code', 'logout','403',- 'emails/reset', 'emails/reset_plaintext'+ 'emails/header', 'emails/footer',+ 'emails/reset', 'emails/reset_plaintext', 'emails/email_confirm', 'emails/email_confirm_plaintext']);}diff --git a/src/user.js b/src/user.jsindex 0bb52ce1a4..fd979933ae 100644--- a/src/user.js+++ b/src/user.js@@ -277,8 +277,35 @@ var config = require('../config.js'),}if (email) {+ var confirm_code = utils.generateUUID(),+ confirm_link = config.url + 'confirm/' + confirm_code,+ confirm_email = global.templates['emails/header'] + global.templates['emails/email_confirm'].parse({'CONFIRM_LINK': confirm_link}) + global.templates['emails/footer'],+ confirm_email_plaintext = global.templates['emails/email_confirm_plaintext'].parse({ 'CONFIRM_LINK': confirm_link });+RDB.set('uid:' + uid + ':email', email);RDB.set('email:' + email, uid);++ // Email confirmation code+ RDB.set('email:' + email + ':confirm', confirm_code, 60*60*2);+ RDB.set('confirm:' + confirm_code + ':email', email, 60*60*2); // Expire after 2 hours++ // Send intro email w/ confirm code+ var message = emailjs.message.create({+ text: confirm_email_plaintext,+ from: config.mailer.from,+ to: email,+ subject: '[NodeBB] Registration Email Verification',+ attachment: [+ {+ data: confirm_email,+ alternative: true+ }+ ]+ });++ emailjsServer.send(message, function(err, success) {+ if (err) console.log(err);+ });}RDB.set('uid:' + uid + ':joindate', new Date().getTime());@@ -485,6 +512,21 @@ var config = require('../config.js'),if (typeof callback !== 'function') socket.emit('user.email.exists', { exists: exists });else callback(exists);});+ },+ confirm: function(code, callback) {+ RDB.get('confirm:' + code + ':email', function(email) {+ if (email !== null) {+ RDB.set('email:' + email + ':confirm', true);+ RDB.del('confirm:' + code + ':email');+ callback({+ status: 'ok'+ });+ } else {+ callback({+ status: 'not_ok'+ });+ }+ });}}diff --git a/src/webserver.js b/src/webserver.jsindex 8fc6c4b687..e1cf20d051 100644--- a/src/webserver.js+++ b/src/webserver.js@@ -140,7 +140,9 @@ passport.deserializeUser(function(uid, done) {res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("' + 'topic/' + req.params.topic_id + '");});</script>' + templates['footer']);});-+ app.get('/confirm/:code', function(req, res) {+ res.send(templates['header'] + '<script>templates.ready(function(){ajaxify.go("' + 'confirm/' + req.params.code + '");});</script>' + templates['footer']);+ });// These functions are called via ajax once the initial page is loaded to populate templates with datafunction api_method(req, res) {@@ -176,6 +178,23 @@ passport.deserializeUser(function(uid, done) {res.send(JSON.stringify(data));}, req.params.id, (req.user) ? req.user.uid : 0);break;+ case 'confirm':+ global.modules.user.email.confirm(req.params.id, function(data) {+ if (data.status === 'ok') {+ res.send(JSON.stringify({+ 'alert-class': 'alert-success',+ title: 'Email Confirmed',+ text: 'Thank you for vaidating your email. Your account is now fully activated.'+ }));+ } else {+ res.send(JSON.stringify({+ 'alert-class': 'alert-error',+ title: 'An error occurred...',+ text: 'There was a problem validating your email address. Perhaps the code was invalid or has expired.'+ }));+ }+ });+ break;default :res.send('{}');break;