Skip to content

Commit

Permalink
Extensive logging in sendmail and forgot password to figure out why i…
Browse files Browse the repository at this point in the history
…t doesn't work as per #3.
  • Loading branch information
asbjornu committed Dec 9, 2015
1 parent a55dd0c commit 7ba92d8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 56 deletions.
89 changes: 46 additions & 43 deletions util/sendmail/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,27 @@ 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');
}
});
};

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');
}
Expand All @@ -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;
}
});
});
};
47 changes: 34 additions & 13 deletions views/login/forgot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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);
}

Expand All @@ -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);
}
});
});
Expand Down

0 comments on commit 7ba92d8

Please sign in to comment.