javascript - Passport 没有保持持久的登录 session

我第一次看 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/

相关文章:

java - 使用 Spring Data 按提取的日期、月份和年份聚合项目组

mongodb - 如何在 MongoDB 的单个集合中查找文档之间的集合交集?

java - MongoDb BSON 以 UTC 时间存储日期

mongodb - Mongo在计算条件下排序

mongodb - 如何在 MongoDB 中获取(或聚合)数组的不同键

node.js - 关于 Mongoose 填充关系一个字符串字段

mongodb - 引用错误 : require is not defined in MongoDB

mongodb - 查询以匹配包含点的多边形

angularjs - angularjs可以直接连接mongodb吗?

node.js - MongoDB Aggregate 中的 Mongoose Virtuals