我对passport local使用mysql数据库有点问题。
尝试登录时收到以下错误消息:
错误:无法将用户序列化到会话中
在/your/path/to/project/auth.js:11:5
这个问题似乎还可以。我得到正确的对象作为响应。
[ RowDataPacket {
id: 1,
firstname: 'firstname',
lastname: 'lastname',
username: 'username',
password: 'password in hash',
role: 'role',
enabled: 1 } ]
但如果我
console.log(user)
在里面
passport.serializeUser
我得到一个
[]
提前感谢:-)
auth.js公司
const passport = require('passport');
const expressSession = require('express-session');
const LocalStrategy = require('passport-local');
const crypto = require('crypto');
const userModel = require('./user/model');
module.exports = app => {
passport.serializeUser(function(user, done){
//console.log(user.id);
done(null, user.id);
})
//passport.serializeUser((user, done) => done(null, user.id));
passport.deserializeUser((id, done) => {
userModel.get({ id }).then(
user => {
if (!user) {
done('user not found');
}
done(null, user);
},
err => done(err),
);
});
passport.use(
new LocalStrategy((username, password, done) => {
const hash = crypto
.createHash('md5')
.update(password)
.digest('hex');
console.log({password: hash});
userModel.get({ username, password: hash }).then(
user => {
if (!user) {
done('user not found');
}
//console.log(user);
done(null, user);
},
error => done('FEHLER: ' + error),
);
}),
);
app.use(
expressSession({
secret: 'geheim',
resave: false,
saveUninitialized: true,
cookie: {secure: true}
}),
);
app.use(passport.initialize());
app.use(passport.session());
app.post(
'/login',
passport.authenticate('local', { failureRedirect: '/login' }),
(request, response) => {
response.redirect('/');
},
);
app.get('/logout', (req, res) => {
req.logout();
res.redirect('/');
});
};
模型.js
'use strict';
const dbServerConnection = require('../controller/mysqlconnector.js');
function get(query = {}) {
return new Promise((resolve, reject) => {
let queryElements = [];
if (query) {
for (let key in query) {
queryElements.push(`${key} = ?`);
}
}
const queryString =
'SELECT * FROM usertable WHERE ' + queryElements.join(' AND ');
dbServerConnection.query(queryString, Object.values(query), (error, results) => {
if (error) {
reject(error);
} else {
console.log(results);
resolve(results);
}
});
});
}
module.exports = {
get,
};
1条答案
按热度按时间sqougxex1#
也许你的
RowDataPacket
不是普通的对象,所以我想你需要修改代码至
?