护照返还"缺少证件"错误

时间:2017-12-19 20:23:48

标签: javascript node.js express passport.js passport-local

新手在这里...我正在使用密码重置表单,因此我只在POST表单中传递了一封电子邮件。我收到了Missing credentials错误消息,而且我甚至没有达到我为此功能制定的策略,因此我无法查询UserModel。可能导致此错误的原因是什么?

我的代码如下:

password_reset_request.html:

<div class="modal fade" id="passwordresetrequest-modal" tabindex="-1" role="dialog" aria-labelledby="passwordresetrequestLabel" aria-hidden="true" style="display: none;">
  <div class="modal-dialog">
        <div class="signup-modal-container">
      <form class="form-signin" action="/password_reset_request" id="password_reset_request_form" method="post">
        <h2 class="form-signin-heading">Request Password Reset for BIDS</h2>
        <label for="email" class="sr-only">Email address</label>
        <input type="email" name="email" id="email" class="form-control" placeholder="Email address" required="" autofocus="">
        <button class="btn btn-lg btn-primary btn-block" name="reset_request" type="submit">Send Password Reset Email</button>
      </form>
    </div>
    </div>
</div>

app.js:

var express = require("express");
var bodyParser = require("body-parser");
var passport = require("passport");
...
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
require("./config/passport")(passport);
...

index.js:

  app.post("/password_reset_request", function(req, res, next) {
    passport.authenticate("local-password-reset-request", function(err, user, info) {
      // NOTE: I'm getting a "null" value for my "err" param.
      // NOTE: the "info" param is where I'm getting my "message: 'Missing credentials'" message.
      if (err) {
        return next(err);
      }
      if (!user) {
        req.flash("error", "Reset failed, no such email.");
        return res.redirect("/");
      }
    })(req, res, next);
  });

passport.js:

  passport.use("local-password-reset-request", new LocalStrategy({
    usernameField: "email",
    passReqToCallback: true
  }, function(req, username, done) {
    new UserModel.User({ email: email }).fetch().then(function(user) {
      // if no user is found, return the message
      if (!user)
        return done(null, false, req.flash("loginMessage", "No user found."));

      var new_password = randomstring.generate({
        length: 12,
        charset: 'alphabetic'
      });

      user
        .set('password', UserModel.generateHash(new_password))
        .save()
        .then(function() {
          console.log('new_password: ' + new_password);
          // Mailer.sendNewPasswordMail(
          //   user.get("email"),
          //   new_password,
          //   req.headers.host
          // );
          return done(null, user);
        })
        ;

      return done(null, user);
    });
  }));

1 个答案:

答案 0 :(得分:0)

当我使用LocalStrategy时,我需要同时提供usernameFieldpasswordField。由于我实际上并没有使用passwordField值,因此我会传递任何非空值并忽略它。