无法重置密码Node.js

时间:2018-01-13 01:46:21

标签: javascript node.js express

编辑1

日志

Sat Jan 13 2018 01:48:56 GMT+0000 (GMT): GET /users/reset-password
Sat Jan 13 2018 01:49:01 GMT+0000 (GMT): POST /users/reset-password
Sat Jan 13 2018 01:49:02 GMT+0000 (GMT): GET /users/reset-password
Sat Jan 13 2018 01:49:06 GMT+0000 (GMT): GET /users/reset-password/d36d4577c0b761a96cbcefd3fd96c3c09ab033b5
Sat Jan 13 2018 01:49:06 GMT+0000 (GMT): GET /users/reset-password/301
Sat Jan 13 2018 01:49:06 GMT+0000 (GMT): GET /users/reset-password
Sat Jan 13 2018 01:49:24 GMT+0000 (GMT): POST /users/reset-password
Sat Jan 13 2018 01:49:25 GMT+0000 (GMT): GET /users/reset-password
Sat Jan 13 2018 01:49:31 GMT+0000 (GMT): GET /users/reset-password/3297b0be778c4e05964e58d1648174af14688f69
Sat Jan 13 2018 01:49:31 GMT+0000 (GMT): GET /users/reset-password/301
Sat Jan 13 2018 01:49:31 GMT+0000 (GMT): GET /users/reset-password

我的令牌似乎在日志中立即过期,没有错误。

还将return res.redirect('/users/new-password');更改为return res.redirect(302, '/users/new-password');

原始问题

我正在实施密码重置,而我似乎无法重置密码。

以下是此问题的GIF

我不太确定我在哪里出错了。

这是一些路线

router.get('/users/reset-password/:token', function(req, res) {
  User.findOne({ resetPasswordToken: req.params.token, resetPasswordExpires: { $gt: Date.now() } }, function(err, user) {
    if (!user) {
      req.flash('error', 'Password reset token is invalid or has expired.');
      return res.redirect('/users/reset-password')
    }

    res.redirect(301, 'reset-password', {
      user: req.user
    });
  });
});

router.get('/users/new-password', function(req, res) {
  res.render('new-password', {
    pageTitle: 'New Password',
    User: req.user
  });
});

router.post('/users/reset-password/:token', function(req, res, next) {
  async.waterfall([
    function(done) {
      User.findOne({ resetPasswordToken: req.params.token, resetPasswordExpires: { $gt: Date.now() } }, function(err, user) {
        if (!user) {
          req.flash('error', 'Password reset token is invalid or has expired.');
          return res.redirect('back');
        }

        user.password = req.body.password;
        user.resetPasswordToken = undefined;
        user.resetPasswordExpires = undefined;

        user.save(function(err) {
          req.logIn(user, function(err) {
            done(err, user);
          });
        });
      });
    },
    function(user, done) {
      nodemailer.createTestAccount((err, account) => {
        // create reusable transporter object using the default SMTP transport
        if (process.env.NODE_ENV === 'production') {
          transporter = nodemailer.createTransport({
            host: "smtp.sendgrid.net",
            port: 587,
            auth: {
              user: process.env.SENDGRID_USERNAME,
              pass: process.env.SENDGRID_PASSWORD,
            }
          });
        } else {
          transporter = nodemailer.createTransport({
            host: "smtp.ethereal.email",
            port: 587,
            auth: {
              user: 'qkkvnabtziufbksa@ethereal.email',
              pass: 'A4W9HF2WbhAav263VM',
            }
          });
        }
        // setup email data with unicode symbols
        let mailOptions = {
          from: 'password.reset' + process.env.GLOBAL_EMAIL || 'ben@benbagley.co.uk', // sender address
          to: user.email, // list of receivers
          subject: 'Your password has been changed', // Subject line
          text: 'Hello,\n\n' + 'This is a confirmation that the password for your account ' + user.email + ' has just been changed.\n' // output
        };
        // send mail with defined transport object
        transporter.sendMail(mailOptions, (err) => {
          req.flash('success', 'Success! Your password has been changed.');
          done(err);
        });
      });
    }
  ], function(err) {
    res.redirect('/');
  });
});

这是新密码视图

{{> header }}

<div class="container">
  {{> flash }}

  <form action="/users/reset-password" method="post">
    <div class="panel panel-default">
      <div class="panel-heading">Reset Password</div>

      <div class="panel-body">
        <div class="form-group">
          <label for="password">New Password</label>
          <input type="password" class="form-control" placeholder="New Password" name="password" value="">
        </div>

        <div class="form-group">
          <label for="confirm">Confirm Password</label>
          <input type="password" class="form-control" placeholder="Confirm Password" name="confirm" value="">
        </div>
        <button type="submit" class="btn btn-primary">Update Password</button>
      </div><!-- Panel Body -->
    </div><!-- Panel Default -->
  </form>
</div><!-- Container ends -->

{{> footer }}

我相信我已经通过了一切,但某些地方出了问题:(

我的代币似乎也会立即过期。

感谢您的任何帮助。

感谢。

0 个答案:

没有答案