mercredi 21 février 2018

Route does not redirect, just crashes. No error is shown

hope your day is going well,

I have this code which is used to reset the password via an email link and return the user back a page with a flash notification for confirmation of completion:

router.post('/resetPassword', function(req, res, next) {
  async.waterfall([
    function(done) {
      crypto.randomBytes(25, function(err, buf) {
        var token = buf.toString('hex');
        done(err, token);
      });
    },
    function(token, done) {
      User.findOne({ email: req.body.username}, function(err, user) {
        if (!user) {
          req.flash('error', 'No account 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 transporter = nodemailer.createTransport({
             service: 'gmail',
             auth: {
                 user: emailAddress,
                 pass: config.emailPass
             }
            });
      var mailOptions = {
        to: user.email,
        from: emailAddress2,
        subject: 'Tracker Password Reset',
        text: 'A request has been made to update the password on the tracking site.\n\n' +
          'Please click on the follow this link to do so:\n\n' +
          'http://' + req.headers.host + '/users/reset/' + token + '\n\n' +
          'If you did not request this, please ignore this email and your password will remain unchanged.\n'
      };

            transporter.sendMail(mailOptions, function(err, info){
             if (error) {
         done(err, 'done');
                 console.log(error);
             } else {
         done(err, 'done');
                 console.log('Email sent: ' + info.response);
             }
            });
    }
  ], function(err) {

    if (err) return next(err);
        req.flash('info', 'An e-mail has been sent to ' + user.email + ' with further instructions.');
    return res.redirect('back');
  });
});

It all runs fine and will send the email e.c.t. but it wont redirect back as I would expect with return res.redirect('back'); , it just gets stuck and eventually crashes.

Thanks for any help, Ed.




Aucun commentaire:

Enregistrer un commentaire