0
當我在字段中輸入不正確的用戶名/密碼時,應用程序崩潰,並且錯誤消息顯示「 req.flash()不是一個函數,它在其他方面工作正常(正確的輸入)。根據CLI的錯誤的原因是在「功能(要求,用戶名,密碼,完成){」但我沒有使用閃光燈也沒有我想,我用我自己的終極版的消息,顯示了我的錯誤消息。有誰知道什麼可能是問題的根源在哪裏?當提交不正確的用戶名/密碼組合時,我的使用護照進行身份驗證的應用程序崩潰(req.flash不是函數)
配置/ 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);
instructorSchema.methods.comparePassword = function(candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if (err) {
return cb(err);
}
cb(null, isMatch);
});
};
這解決的一個問題,但新的錯誤是錯誤:req.flash()要求 – fj785
您需要安裝快車的會話NPM包,並在項目中導入會議。 –