护照登录无法正常工作。 this._verify 不是函数

passport login is not working correctly. this._verify is not a function

这里需要一些帮助。我在快速申请中使用护照。我可以注册一个用户,但是一旦我尝试登录,我就会收到以下错误。

TypeError: this._verify is not a function
    at Strategy.authenticate (/mnt/c/Users/Dell/Documents/StateOfSnowPack/node_modules/passport-local/lib/strategy.js:90:12)
    at attempt (/mnt/c/Users/Dell/Documents/StateOfSnowPack/node_modules/passport/lib/middleware/authenticate.js:366:16)

这很奇怪,因为这个错误是在我没有更改任何登录功能的情况下才开始发生的。

这是我的代码片段。

app.js:

app.use(passport.initialize());
app.use(passport.session());
 passport.use(new LocalStrategy({
    usernameField: 'email',
}, User.createStrategy()));


passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

users.js:

router.post('/login', passport.authenticate('local', { failureFlash: 'invalid username or password', failureRedirect: '/login'}), (req, res, err) => {
    req.login();
    req.flash('success', 'Welcome Back!');
    const redirectUrl = '/';
    res.redirect(redirectUrl);
}
     );

我的策略:

function Strategy(options, verify) {
  if (typeof options == 'function') {
    verify = options;
    options = {};
    console.log(verify);
  }
  if (!verify) { console.log('verify doesnt exist'); throw new TypeError('LocalStrategy requires a verify callback'); }
  
  this._usernameField = options.usernameField || 'username';
  this._passwordField = options.passwordField || 'password';
  
  passport.Strategy.call(this);
  this.name = 'local';
  this._verify = verify;
  this._passReqToCallback = options.passReqToCallback;
}

/**
 * Inherit from `passport.Strategy`.
 */
util.inherits(Strategy, passport.Strategy);

/**
 * Authenticate request based on the contents of a form submission.
 *
 * @param {Object} req
 * @api protected
 */
Strategy.prototype.authenticate = function(req, options) {
  options = options || {};
  var username = lookup(req.body, this._usernameField) || lookup(req.query, this._usernameField);
  var password = lookup(req.body, this._passwordField) || lookup(req.query, this._passwordField);
  
  if (!username || !password) {
    return this.fail({ message: options.badRequestMessage || 'Missing credentials' }, 400);
  }
  
  var self = this;
  
  function verified(err, user, info) {
    if (err) { return self.error(err); }
    if (!user) { return self.fail(info); }
    self.success(user, info);
  }
  
  try {
    if (self._passReqToCallback) {
      this._verify(req, username, password, verified);
    } else {
      this._verify(username, password, verified);
    }
  } catch (ex) {
    return self.error(ex);
  }
};


/**
 * Expose `Strategy`.
 */
module.exports = Strategy;

try(self._passReqToCallback) 块被执行。然而,当我 console.log(this) or (self) 我得到:

Strategy {
  success: [Function (anonymous)],       
  fail: [Function (anonymous)],
  redirect: [Function (anonymous)],      
  pass: [Function (anonymous)],
  error: [Function (anonymous)]
}

我最终解决了问题。我必须指定我自己的本地策略对象而不是使用 User.createStrategy。我现在可以在没有 运行 任何错误的情况下登录。