问题在于当我登录时,passport会运行其反序列化函数多次。虽然这对事情没有任何影响,但是以后可能会有问题。以下是日志:
Bloodmorphed has been Serialized
Bloodmorphed has been deserialized
Bloodmorphed has been deserialized
Bloodmorphed has been deserialized
Bloodmorphed has been deserialized
Bloodmorphed has been deserialized
Bloodmorphed has been deserialized
这是护照:
/*jshint esversion: 6 */
const LocalStrategy = require('passport-local').Strategy;
const db = require('../config/db');
const bcrypt = require('bcryptjs');
let io = require('./io');
module.exports = (passport) => {
// =========================================================================
// passport session setup ==================================================
// =========================================================================
// used to serialize the user for the session
passport.serializeUser((user, done) => {
console.log(user.username + ' has been Serialized');
done(null, user.id);
});
// used to deserialize the user
passport.deserializeUser((id, done) => {
let sql = 'SELECT * FROM users, users_meta WHERE users.id = ? AND users_meta.id =?';
db.query(sql, [id, id]).then(results => {
var userdata = results[0];
console.log(userdata.username + ' has been deserialized');
done(null, userdata);
});
});
// Local Strategy login
passport.use('local-login', new LocalStrategy({
passReqToCallback: true,
}, (req, username, password, done) => {
// Match Username
let sql = 'SELECT * FROM users WHERE username = ?';
db.query(sql, [username]).then(results => {
if (!results.length) {
return done(null, false, {
type: 'loginMessage',
message: 'Wrong Login',
});
}
// Match Password
bcrypt.compare(password, results[0].password, (err, isMatch) => {
if (isMatch) {
var userData = results[0];
sql = 'SELECT * FROM users_meta WHERE id = ?';
db.query(sql, userData.id).then(results => {
Object.assign(userData, results[0]);
return done(null, userData);
});
} else {
return done(null, false, {
type: 'loginMessage',
message: 'Wrong Login',
});
}
});
});
}));
};
虽然目前这不是一个高优先级的问题,但我希望能够解决它,或者确认这是否是正常现象。