diff --git a/util/sendmail/index.js b/util/sendmail/index.js index dc641c872..2577c6c16 100644 --- a/util/sendmail/index.js +++ b/util/sendmail/index.js @@ -16,11 +16,13 @@ exports = module.exports = function(req, res, options) { } */ var renderText = function(callback) { + req.app.utility.debug('util.sendmail.renderText'); + res.render(options.textPath, options.locals, function(err, text) { if (err) { + req.app.utility.error('util.sendmail.renderText:', err); callback(err, null); - } - else { + } else { options.text = text; return callback(null, 'done'); } @@ -28,11 +30,13 @@ exports = module.exports = function(req, res, options) { }; var renderHtml = function(callback) { + req.app.utility.debug('util.sendmail.renderHtml'); + res.render(options.htmlPath, options.locals, function(err, html) { if (err) { + req.app.utility.error('util.sendmail.renderHtml:', err); callback(err, null); - } - else { + } else { options.html = html; return callback(null, 'done'); } @@ -48,47 +52,46 @@ exports = module.exports = function(req, res, options) { renderers.push(renderHtml); } - require('async').parallel( - renderers, - function(err, results){ - if (err) { - options.error('Email template render failed. '+ err); - return; - } + require('async').parallel(renderers, function(err, results) { + if (err) { + req.app.utility.error('util.sendmail.async.parallel:', err); + options.error('Email template render failed. ' + err); + return; + } - var attachments = []; + var attachments = []; - if (options.html) { - attachments.push({ data: options.html, alternative: true }); - } + if (options.html) { + attachments.push({ data: options.html, alternative: true }); + } - if (options.attachments) { - for (var i = 0 ; i < options.attachments.length ; i++) { - attachments.push(options.attachments[i]); - } + if (options.attachments) { + for (var i = 0; i < options.attachments.length; i++) { + attachments.push(options.attachments[i]); } - - var emailjs = require('emailjs/email'); - var emailer = emailjs.server.connect( req.app.config.smtp.credentials ); - emailer.send({ - from: options.from, - to: options.to, - 'reply-to': options.replyTo || options.from, - cc: options.cc, - bcc: options.bcc, - subject: options.subject, - text: options.text, - attachment: attachments - }, function(err, message) { - if (err) { - options.error('Email failed to send. '+ err); - return; - } - else { - options.success(message); - return; - } - }); } - ); -}; + + var emailjs = require('emailjs/email'); + var emailer = emailjs.server.connect(req.app.config.smtp.credentials); + emailer.send({ + from: options.from, + to: options.to, + 'reply-to': options.replyTo || options.from, + cc: options.cc, + bcc: options.bcc, + subject: options.subject, + text: options.text, + attachment: attachments + }, function(err, message) { + if (err) { + req.app.utility.error('util.sendmail.emailer.send:', err); + options.error('Email failed to send. ' + err); + return; + } else { + req.app.utility.debug('util.sendmail.emailer.send:', message); + options.success(message); + return; + } + }); + }); +}; \ No newline at end of file diff --git a/views/login/forgot/index.js b/views/login/forgot/index.js index 33266e140..341f94985 100644 --- a/views/login/forgot/index.js +++ b/views/login/forgot/index.js @@ -3,17 +3,21 @@ exports.init = function(req, res) { if (req.isAuthenticated()) { res.redirect(req.user.defaultReturnUrl()); - } - else { + } else { res.render('login/forgot/index'); } }; -exports.send = function(req, res, next){ +exports.send = function(req, res, next) { + req.app.utility.debug('login.forgot.send:', req.body.email); + var workflow = req.app.utility.workflow(req, res); workflow.on('validate', function() { + req.app.utility.debug('login.forgot.workflow.validate:', req.body.email); + if (!req.body.email) { + req.app.utility.error('login.forgot.workflow.validate: Email required'); workflow.outcome.errfor.email = 'required'; return workflow.emit('response'); } @@ -22,16 +26,22 @@ exports.send = function(req, res, next){ }); workflow.on('generateToken', function() { + req.app.utility.debug('login.forgot.workflow.generateToken'); + var crypto = require('crypto'); crypto.randomBytes(21, function(err, buf) { if (err) { + req.app.utility.error('login.forgot.workflow.generateToken.randomBytes', err); return next(err); } var token = buf.toString('hex'); req.app.db.User.encryptPassword(token, function(err, hash) { + req.app.utility.debug('login.forgot.workflow.generateToken.encryptPassword:', hash); + if (err) { + req.app.utility.error('login.forgot.workflow.generateToken.encryptPassword:', err); return next(err); } @@ -43,40 +53,51 @@ exports.send = function(req, res, next){ workflow.on('patchUser', function(token, hash) { var email = req.body.email.toLowerCase(); - req.app.utility.debug('Workflow.PatchUser:', token, hash, email); + req.app.utility.debug('login.forgot.workflow.patchUser:', token, hash, email); req.app.db.User .findOne({ where : { email: email } }) .then(function(user) { + req.app.utility.debug('login.forgot.workflow.patchUser.findOne:', email); + if (user) { + req.app.utility.debug('login.forgot.workflow.patchUser.findOne:', user); user.resetPasswordToken = hash; user.resetPasswordExpires = Date.now() + 10000000; user.save().then(function() { workflow.emit('sendEmail', token, user); }); + } else { + req.app.utility.debug('login.forgot.workflow.patchUser.findOne: No user found!', email); } - return workflow.emit('response'); - }, function(err) { - return workflow.emit('exception', err); - }); + return workflow.emit('response'); + }).catch(function(err) { + req.app.utility.error('login.forgot.workflow.patchUser:', err); + return workflow.emit('exception', err); + }); }); workflow.on('sendEmail', function(token, user) { + req.app.utility.debug('login.forgot.workflow.sendEmail:', token, user); + req.app.utility.sendmail(req, res, { - from: req.app.config.smtp.from.name +' <'+ req.app.config.smtp.from.address +'>', + from: req.app.config.smtp.from.name + ' <' + req.app.config.smtp.from.address + '>', to: user.email, - subject: 'Reset your '+ req.app.config.projectName +' password', + subject: 'Reset your ' + req.app.config.projectName + ' password', textPath: 'login/forgot/email-text', htmlPath: 'login/forgot/email-html', locals: { username: user.username, - resetLink: req.protocol +'://'+ req.headers.host +'/login/reset/'+ user.email +'/'+ token +'/', + resetLink: req.protocol + '://' + req.headers.host + '/login/reset/' + user.email + '/' + token + '/', projectName: req.app.config.projectName }, - success: function(message) {}, + success: function(message) { + req.app.utility.debug('login.forgot.workflow.sendEmail.success:', message); + }, error: function(err) { - workflow.outcome.errors.push('Error Sending: '+ err); + req.app.utility.error('login.forgot.workflow.sendEmail.error:', err); + workflow.outcome.errors.push('Error Sending: ' + err); } }); });