0
votes

When I input an incorrect username/pw into my fields, the app crashes and the error message says "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) {" but I am not using flash nor do I want to. I am using my own redux message that shows for my error messages. Does anyone know what could be the root of the problem?

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);
Relevant code in instructor schema
instructorSchema.methods.comparePassword = function(candidatePassword, cb) {
 bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if (err) {
  return cb(err);
}

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

1 Answers

1
votes

You're not passing the req object to the jwt strategy. Try something like this:

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