我在将用户序列化为会话时遇到问题。当我将会话设置为false时,它可以很好地工作。我不确定我的“ deserializeUser”和“ serializerUser”代码是否正确。我收到错误消息:Failed to serialize user into session
。我不介意使用任何东西(ID或电子邮件)进行序列化。
passport.js
module.exports = function(passport) {
passport.use(
new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
// Match email
db.query("select email, password from register where email = ? ",
email, function(err, data){
if(!data){
return done(null, false, { message: 'That email is not registered' });
}
db.query('select password from register', function (err, rs) {
bcrypt.compare(password, rs[0].password, (err, isMatch)=>{
if(err) throw err;
if(isMatch){
return done(null, data );
}
else{
return done(null, false, {message: 'Password is not correct'});
}
});
});
});
})
);
passport.serializeUser(function(data, done) {
db.query("select * from register where email = ?", [data],
function(err, rows){
done(null,rows[0]);
console.log(rows[0]);
});
});
passport.deserializeUser(function(data, done) {
db.query("select * from register where email = ?", [data],
function(err, rows){
done(err,rows[0]);
});
});
};
index.js
router.post('/login', function(req, res, next) {
passport.authenticate('local',{
successRedirect: '/getme',
failureRedirect: '/login',
failureFlash: true
}) (req, res, next);
});