我第一次看 persistent sessions with passport, mongodb and express但它没有帮助或意义。
我正在尝试使用我的 website 获得持久登录。 .我的序列化过程不起作用。
// Passport needs to be able to serialize and deserialize users to support persistent login sessions
passport.serializeUser(function(user, done) {
console.log('serializing user:',user.username);
//return the unique id for the user
return done(null, user._id);
});
//Desieralize user will call with the unique id provided by serializeuser
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
console.log('deserializing user:',user.username);
return done(err, user);
});
});
整个 Passport 文件可以在github上找到。
我认为问题在于我会立即反序列化,或者至少是 console.logs 显示的内容。
或者它可能与我的 session 有关:
app.use(session({
secret: 'keyboard cat',
cookie : {
maxAge: 3600000 // see below
}
}));
这是我的用户架构:
var userSchema = new mongoose.Schema({
username : String,
password : String, //Hash
created_at : {type: Date, default : Date.now}
});
感谢您的帮助!
最佳答案
你提到的链接,persistent sessions with passport, mongodb and express , 正在谈论 express 框架的旧版本,您在 package.json
中使用的那个,https://github.com/manu354/teecher/blob/master/package.json , "express": "~4.13.1"
,很新。
你需要移动这些行:
app.use(passport.initialize());
app.use(passport.session());
稍稍高于app.use(session({...})
我建议您关注这篇博文,http://mherman.org/blog/2015/01/31/local-authentication-with-passport-and-express-4/ ,一定能帮到你
https://stackoverflow.com/questions/33595795/