【问题标题】:persistent sessions with passport, mongodb and express与护照、mongodb 和 express 的持久会话
【发布时间】:2013-04-10 07:40:44
【问题描述】:

我正在使用护照来处理我的应用程序中的身份验证和会话。我正在使用 mongostore 将会话持久化到 mongodb。

该设置通常运行良好。但是,当我重新启动服务器时,所有用户都已注销,因此显然会话保留在内存中,而不是仅保留到 mongodb。我正在尝试实现在重新启动服务器时用户仍处于登录状态的设置。

基本配置如下

中间件

    app.use(express.cookieParser('your secret here'));
    app.use(express.session());
    app.use(passport.initialize());
    app.use(passport.session({
        maxAge: new Date(Date.now() + 3600000),
        store: new MongoStore(
            {
                db: mongodb.Db(
                    conf.mongodbName,
                    new mongodb.Server(
                        'localhost',
                        27017,
                        {
                            auto_reconnect: true,
                            native_parser: true
                        }
                    ),
                    {
                        journal: true
                    }
                )
            },
            function(error) {
                if(error) {
                    return console.error('Failed connecting mongostore for storing session data. %s', error.stack);
                }
                return console.log('Connected mongostore for storing session data');
            }
        )
    }));

护照

passport.use(new LocalStrategy(
    {
        usernameField: 'email',
        passwordField: 'password'
    },
    function(email, password, done) {
        console.log('user %s attempting to authenticated', email);
        return User.findOne({email:email}, function(error, user) {
            if(error) {
                console.error('Failed saving user %s. %s', user.id, error.stack);
                return done(error);
            }
            if(!user) {
                return done(null, false);
            }
            console.log('user %s logged in successfully', user.id);
            return done(null, { //passed to callback of passport.serializeUser
                id : user.id
            });
        });
    }
));

passport.serializeUser(function(user, done) {
    return done(null, user.id); //this is the 'user' property saved in req.session.passport.user
});

passport.deserializeUser(function (id, done) {
    return User.findOne({ id: id }, function (error, user) {
        return done(error, user);
    });
});

github repo(包括运行代码所需的所有代码)

我创建了一个准系统 github 存储库,其中包含代码 here

只需使用您的 mongodb 凭据(即 mongodbURL 和 mongodbName)在根目录中创建一个 conf.js 文件,运行 npm install 和 node app.js 即可开始。

谢谢

【问题讨论】:

标签: node.js mongodb express passport.js


【解决方案1】:

passport.session() 不需要任何配置,从 Express 版本 4.X 开始,您需要配置 session()

app.use(session({
  cookie : {
    maxAge: 3600000 // see below
  },
  store : new MongoStore(...)
});
...
app.use(passport.session());

另外,maxAge(应该是cookie 的属性)不接受Date 参数,而只是会话应该有效的毫秒数。

有关使用 express 中间件模块会话的说明,您可以找到更多here

【讨论】:

    猜你喜欢
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    相关资源
    最近更新 更多