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.
*/
var async = require('async');
var bcrypt = require('bcrypt-nodejs');
var crypto = require('crypto');
var mongoose = require('mongoose');
@ -72,18 +72,8 @@ exports.getForgot = 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();
var errors = req.validationErrors();
if (errors) {
@ -91,63 +81,38 @@ exports.postForgot = function(req, res) {
return res.redirect('/forgot');
}
// next step
workflow.emit('generateToken');
});
async.waterfall([
function(done) {
/**
* Step 2: Generate a one-time (nonce) token
* Generate a one-time token.
*/
workflow.on('generateToken', function() {
// generate token
crypto.randomBytes(24, function(err, buf) {
if (err) return next(err);
crypto.randomBytes(32, function(err, buf) {
var token = buf.toString('base64');
console.log(token);
workflow.emit('saveToken', token)
done(err, 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) {
if (err) {
req.flash('errors', err);
return res.redirect('/forgot');
}
if (!user) {
// If we didn't find a user associated with that
// 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!' });
req.flash('errors', { msg: 'No account with that email address exists.' });
return res.redirect('/forgot');
}
user.resetPasswordToken = token;
user.resetPasswordExpires = Date.now() + 3600000; // 1 hour
// update the user's record with the token
user.save(function(err) {
if (err) {
req.flash('errors', err);
return res.redirect('/forgot');
}
});
// next step
workflow.emit('sendEmail', token, user);
done(err, 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', {
service: 'SendGrid',
auth: {
@ -155,7 +120,6 @@ exports.postForgot = function(req, res) {
pass: secrets.sendgrid.password
}
});
var mailOptions = {
to: user.profile.name + ' <' + user.email + '>',
from: 'hackathon@starter.com',
@ -165,27 +129,11 @@ exports.postForgot = function(req, res) {
'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'
};
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.' });
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');
]);
};