当提交不正确的 username/password 组合时,我使用护照进行身份验证的应用程序崩溃(req.flash 不是函数)

My app which uses passport for authentication crashes when an incorrect username/password combo is submitted (req.flash is not a function)

当我在我的字段中输入不正确的 username/pw 时,应用程序崩溃并且错误消息显示 "req.flash() is not a function. It works fine otherwise(correct input). The cause of the error according to the CLI is at "function(req, username, password, done) {" 但我没有使用 flash我也不想。我正在使用我自己的 redux 消息显示我的错误消息。有谁知道问题的根源是什么?

config/passport.js

const passport = require('passport');
const Instructor = require('../models/instructor');
const config = require('./main');
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const LocalStrategy = require('passport-local');

const localLogin = new LocalStrategy(
  {
    passReqToCallback: true,
  },
  function(req, username, password, done) {
    console.log('This is getting called!');
    Instructor.findOne({ username: username }, function(err, instructor) {
      if (err) {
        return done(err);
      }
      if (!instructor) {
        return done(null, false, {
          error: 'Your login details could not be verified. Please try again.',
        });
      }

      instructor.comparePassword(password, function(err, isMatch) {
        if (err) {
          return done(err);
        }
        if (!isMatch) {
          return done(null, false);
        }
        console.log('Success!');
        return done(null, instructor);
      });
    });
  }
);

const jwtOptions = {
  // Telling Passport to check authorization headers for JWT
  jwtFromRequest: ExtractJwt.fromAuthHeader(),
  // Telling Passport where to find the secret
  secretOrKey: config.jwt,
};

const jwtLogin = new JwtStrategy(jwtOptions, function(payload, done) {
  Instructor.findById(payload._id, function(err, instructor) {
    if (err) {
      return done(err, false);
    }

    if (instructor) {
      done(null, instructor);
    } else {
      done(null, false);
    }
  });
});
passport.use(jwtLogin);
passport.use(localLogin);
instructor 模式中的相关代码

instructorSchema.methods.comparePassword = function(candidatePassword, cb) {
 bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if (err) {
  return cb(err);
}

cb(null, isMatch);
  });
};

您没有将 req 对象传递给 jwt strategy。尝试这样的事情:

const jwtLogin = new JwtStrategy(jwtOptions, function(req, payload, done) { ... }