Converted workflow/eventemitter code to async.waterfall

This commit is contained in:
Sahat Yalkabov
2014-02-18 02:09:51 -05:00
parent 6549966a16
commit 76a73943f4

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies. * Module dependencies.
*/ */
var async = require('async');
var bcrypt = require('bcrypt-nodejs'); var bcrypt = require('bcrypt-nodejs');
var crypto = require('crypto'); var crypto = require('crypto');
var mongoose = require('mongoose'); var mongoose = require('mongoose');
@ -72,18 +72,8 @@ exports.getForgot = function(req, res) {
*/ */
exports.postForgot = function(req, res) { exports.postForgot = function(req, res) {
// Begin a workflow
var workflow = new (require('events').EventEmitter)();
/**
* Step 1: Is the email valid?
*/
workflow.on('validate', function() {
// Check for form errors
req.assert('email', 'Please enter a valid email address.').isEmail(); req.assert('email', 'Please enter a valid email address.').isEmail();
var errors = req.validationErrors(); var errors = req.validationErrors();
if (errors) { if (errors) {
@ -91,63 +81,38 @@ exports.postForgot = function(req, res) {
return res.redirect('/forgot'); return res.redirect('/forgot');
} }
// next step async.waterfall([
workflow.emit('generateToken'); function(done) {
});
/** /**
* Step 2: Generate a one-time (nonce) token * Generate a one-time token.
*/ */
crypto.randomBytes(32, function(err, buf) {
workflow.on('generateToken', function() {
// generate token
crypto.randomBytes(24, function(err, buf) {
if (err) return next(err);
var token = buf.toString('base64'); var token = buf.toString('base64');
console.log(token); done(err, token);
workflow.emit('saveToken', token)
}); });
}); },
function(token, done) {
/** /**
* Step 3: Save the token and token expiration * Save the token and token expiration.
*/ */
workflow.on('saveToken', function(token) {
// lookup user
User.findOne({ email: req.body.email.toLowerCase() }, function(err, user) { User.findOne({ email: req.body.email.toLowerCase() }, function(err, user) {
if (err) {
req.flash('errors', err);
return res.redirect('/forgot');
}
if (!user) { if (!user) {
// If we didn't find a user associated with that req.flash('errors', { msg: 'No account with that email address exists.' });
// email address then just finish the workflow
req.flash('info', { msg: 'If you have an account with that email address then we sent you an email with instructions. Check your email!' });
return res.redirect('/forgot'); return res.redirect('/forgot');
} }
user.resetPasswordToken = token; user.resetPasswordToken = token;
user.resetPasswordExpires = Date.now() + 3600000; // 1 hour user.resetPasswordExpires = Date.now() + 3600000; // 1 hour
// update the user's record with the token
user.save(function(err) { user.save(function(err) {
if (err) { done(err, token, user);
req.flash('errors', err);
return res.redirect('/forgot');
}
});
// next step
workflow.emit('sendEmail', token, user);
}); });
}); });
},
function(token, user, done) {
/** /**
* Step 4: Send the user an email with a reset link * Send the user an email with a reset link.
*/ */
workflow.on('sendEmail', function(token, user) {
var smtpTransport = nodemailer.createTransport('SMTP', { var smtpTransport = nodemailer.createTransport('SMTP', {
service: 'SendGrid', service: 'SendGrid',
auth: { auth: {
@ -155,7 +120,6 @@ exports.postForgot = function(req, res) {
pass: secrets.sendgrid.password pass: secrets.sendgrid.password
} }
}); });
var mailOptions = { var mailOptions = {
to: user.profile.name + ' <' + user.email + '>', to: user.profile.name + ' <' + user.email + '>',
from: 'hackathon@starter.com', from: 'hackathon@starter.com',
@ -165,27 +129,11 @@ exports.postForgot = function(req, res) {
'http://' + req.headers.host + '/reset/' + token + '\n\n' + 'http://' + req.headers.host + '/reset/' + token + '\n\n' +
'If you did not request this, please ignore this email and your password will remain unchanged.\n' 'If you did not request this, please ignore this email and your password will remain unchanged.\n'
}; };
smtpTransport.sendMail(mailOptions, function(err) { smtpTransport.sendMail(mailOptions, function(err) {
if (err) {
req.flash('errors', { msg: err.message });
return res.redirect('/forgot');
} else {
// Message to user
req.flash('info', { msg: 'We have sent an email to ' + user.email + ' for further instructions.' }); req.flash('info', { msg: 'We have sent an email to ' + user.email + ' for further instructions.' });
return res.redirect('/forgot'); done(err, 'done');
res.redirect('/forgot');
});
} }
}); ]);
// shut down the connection pool, no more messages
smtpTransport.close();
});
/**
* Initiate the workflow
*/
workflow.emit('validate');
}; };