【问题标题】:Node.js password resetNode.js 密码重置
【发布时间】:2016-06-12 10:56:16
【问题描述】:

我一直致力于在 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);

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

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

【问题讨论】:

    标签: javascript node.js express forgot-password


    【解决方案1】:

    提交 from 将导航到表单的操作。您可以将“onclick”添加到按钮,以使您保持在同一页面中,并从中发送 ajax 请求。这样你就可以处理ajax成功了。

    【讨论】:

      猜你喜欢
      • 2016-10-28
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      • 2011-04-03
      • 2013-12-15
      • 1970-01-01
      相关资源
      最近更新 更多