Node.js密码重置

时间:2016-02-29 11:57:38

标签: javascript node.js express forgot-password

我一直致力于Node.js和Express.js中的密码重置 到目前为止,我的代码正在向用户发送电子邮件以更改密码。此外,用户可以更改密码并在更改密码后收到电子邮件。 现在,问题是,我提交密码后无法显示成功消息。按下提交按钮后,它会重定向到reset.ejs页面。 以下是我的代码,

reset.ejs

<html>
<body style="margin-left:0px;">
    <div class="custom-header">
        <div class="custom-header-left">

        </div>
        <div class="custom-header-button">

        </div>
        <div class="custom-header-right">
            <a class="navbar-brand">
                <img alt="AGD" src="agdlogo.png" style="width:70%; margin-top:-22%;">
            </a>
            <a class="navbar-brand" href="/"> XXX &nbsp;</a>
            <a class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </a>
        </div>
    </div>

    <!-- Top Bar-->

    <div id="fullpage">
        <div class="">
            <div class="col-md-12  custom-margin-top">
                <div class="panel panel-primary text-left">
                    <div class="panel-heading">Reset your Password</div>
                    <div class="panel-body">
                        <div class="col-md-10 ">
                            <form id="resetpass" role="form" class="form">
                                <div>
                                    <div class="form-group">
                                        <label for="InputEmail">New Password</label>
                                        <input type="password" class="form-control" id="password" class="password" name="password" placeholder="Enter Email" required>
                                        <label for="InputEmail">Confirm Password</label>
                                        <input type="password" class="form-control" id="repass" class="repass" name="repass" placeholder="Enter Email" required>
                                        <!--    <input type="hidden" name="token" id="token">-->
                                        <button type="submit">Submit</button>
                                    </div>
                            </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
</body>


<script>
    var params = location.href;
    var paramsplit = params.split('/');
    //console.log(paramsplit);
    $("#resetpass").submit(function (e) {
        var resetData = {
            password: $("#password").val(),
            repass: $("#repass").val(),
            token: paramsplit[4]
        }

        console.log(resetData);

        // console.log(resetData);
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: '/reset/:token',
            data: JSON.stringify(resetData),
            success: function (data) {
                //console.log(data); // show response from the php script.
            }
        });

        // e.preventDefault(); // avoid to execute the actual submit of the form.
    });
</script>
<style CSS>
    @import url("https://bootswatch.com/flatly/bootstrap.min.css");
</style>
</html>

我有一个mail.controller.js文件,用于控制视图和模型

exports.resetpassword = function(req, res) {
  var data = req.body;

  async.waterfall([
    function(done) {
      User.findOne({
        resetPasswordToken: req.body.token,
        resetPasswordExpires: {
          $gt: Date.now()
        }
      }, function(err, user) {
        if (!user) {
          res.render('tinypage/regnotify', {
            title: "Something is wrong",
            alerttype: "alert-danger",
            message: "Something wrong with your password change."
          });
        } else {
          user.password = req.body.password;
          user.resetPasswordToken = '';
          user.resetPasswordExpires = '';
          user.save(function(err, user) {
            done(err, user);
          });
        }
      });
    },

    function(user, done) {
      var smtpTransport = nodemailer.createTransport('SMTP', {
        service: 'Mailgun',
        auth: {
          user: 'sdfa',
          pass: 'afdafsa'
        }
      });

      var mailOptions = {
        to: user.email,
        from: 'agdtrack@s.com',
        subject: 'Your password has been changed',
        text: 'Hello,\n\n' +
          'This is a confirmation that the password for your account ' + req.body.token + ' has just been changed.\n'
      };

      smtpTransport.sendMail(mailOptions, function(err) {
        if (err) {
          res.render('tinypage/regnotify', {
            title: "Wrong",
            alerttype: "alert-danger",
            message: "Something wrong"
          });
        } else {
          return res.render('tinypage/regnotify', {
            title: "Success",
            alerttype: "alert-success",
            message: "Success! Your password has been changed."
          });
          done(err);
        }
      });
    }
  ], function(err) {
    res.redirect('/');
  });
};

exports.renderresetpage = function(req, res) {
  res.render('reset');
};

我的mail.route.js是:

app.route('/reset/:token').get(mail.renderresetpage);    
app.route('/reset/:token').post(mail.resetpassword);

一切正常。按下提交按钮后才显示成功消息。

您的贡献将是一个很大的帮助。

1 个答案:

答案 0 :(得分:0)

提交from将导航到表单的操作。您可以在按钮上添加“onclick”,而不是让您进入同一页面,并从中发送ajax请求。所以你可以处理ajax的成功。