持续会议与护照,蒙哥大和快递 [英] persistent sessions with passport, mongodb and express

查看:204
本文介绍了持续会议与护照,蒙哥大和快递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用护照来处理我的应用程序中的身份验证和会话。我正在使用mongostore继续使用mongodb。

I'm using passport to handle authentication and sessions in my application. I'm persisting sessions to mongodb using mongostore.

一般来说,设置工作正常。但是,当我重新启动服务器时,所有用户都会注销,所以显然会话保留在内存中,而不是只保留到mongodb。我正在尝试实现一个用户在重新启动服务器时仍然登录的设置。

The setup works fine in general. However, when I restart the server all users are logged out, so apparently sessions are hold in memory instead of being only persisted to mongodb. I'm trying to achieve a setup where users are still logged in when restarting the server.

基本配置如下

    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

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 repo,其中包含代码 here

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

just create a conf.js file in the root directory with your mongodb credentials, i.e. mongodbURL and mongodbName, run npm install and node app.js to get started.

谢谢

推荐答案

passport.session()你需要配置:

passport.session() doesn't take any configuration, as of Express version 4.X it's session() you need to configure:

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

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

Also, maxAge (which should be a property of cookie) doesn't take a Date argument, but just the number of milliseconds a session should be valid.

有关使用快速中间件模块会话的说明,您可以找到更多此处

For instructions on using the express middleware module session, you can find out more here.

这篇关于持续会议与护照,蒙哥大和快递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆