gpt4 book ai didi

mysql - Passport 错误 : Failed to serialize user into session

转载 作者:行者123 更新时间:2023-11-29 07:27:58 25 4
gpt4 key购买 nike

我对使用 mySQL-DB 的 passport-local 有点问题。

当我尝试登录时收到以下错误消息:

Error: Failed to serialize user into session

at /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('/');
});
};

model.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,
};

最佳答案

也许你的 RowDataPacket 不是普通对象,所以我想你需要更改代码

dbServerConnection.query(queryString, Object.values(query), (error, results) => {
if (error) {
reject(error);
} else {
console.log(results);
resolve(results);
}
});

dbServerConnection.query(queryString, Object.values(query), (error, results) => {
if (error) {
reject(error);
} else {
resolve(results[0]);
}
});

?

关于mysql - Passport 错误 : Failed to serialize user into session,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52928359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com