我将 Nodejs 与 Passport.js 结合使用。一旦我在 localhost:3000
上运行我的服务器,我就不断得到
Error: failed to deserialize user out of session
我在 this 上找到了解决方案Github 线程。
passport.deserializeUser(function(obj, done) {
done(null, false); // invalidates the existing login session.
});
来自线程:
As things stand now, as soon as you get a deserialization error you're essentially blacklisted
这可能是我对 session 的理解不佳,但即使我重新启动服务器,我仍然收到上述错误。我在哪里被列入黑名单?重新启动服务器不应该清除所有服务器 session 吗?
最佳答案
你应该这样做
passport.serializeUser(function (user, done) {
// this user is from your passport strategy
// for example: https://github.com/jaredhanson/passport-local#user-content-configure-strategy
// the data you give will store into session
done(null, user._id);
});
passport.deserializeUser(function (id, done) {
// the id is from session which store by serializeUser function
// this function should varify user and return its data to passport
// when passport jobs done
// you can access user data from req.user in express; ctx.state.user in koa
1User.findById(id, function (err, user) {
done(err, user); // if user is null, will block user
});
});
关于node.js - Passport js反序列化黑名单错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56051334/