I'm trying to add a reset password function to my app. I've created a basic form where the user fills in their email and this should then generate an email to that user's address. That seems to be working, however my code seems to be freezing before I reach the redirect function, so the email is generated but the user never gets redirected - instead the page just hangs. Here's my code which runs once the user submits the forgotten password form:
router.post('/forgot', function(req, res, next) {
async.waterfall([
function(done) {
crypto.randomBytes(20, function(err, buf) {
var token = buf.toString('hex');
done(err, token);
});
},
function(token, done) {
User.findOne({ email: req.body.email }, function(err, user) {
if(!user) {
req.flash('error', 'No acccount with that email address exists.');
return res.redirect('/forgot');
}
user.resetPasswordToken = token;
user.resetPasswordExpires = Date.now() + 3600000; // 1 hour
user.save(function(err) {
done(err, token, user);
});
});
},
function(token, user, done) {
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: "*****@gmail.com", // my actual email address here
pass: "********" // my actual password here
}
});
var mailOptions = {
to: user.email,
from: 'dvbage@gmail.com',
text: 'Please click on the following linke to reset password:'
};
smtpTransport.sendMail(mailOptions, function(err) {
req.flash('info', 'An e-mail has been sent to ' + user.email + ' with instructions as to how to change your password')
});
}
], function(err) {
if(err) return next(err);
res.redirect('/forgot');
});
});
I tried throwing some debuggers in but didn't seem to work. Anyone have any ideas?
via DaveB1
No comments:
Post a Comment