javascript 登录passportjs后如何设置会话超时?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17991050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:22:25  来源:igfitidea点击:

How to set the session timeout after log in passportjs?

javascriptnode.jsexpresspassport.js

提问by Jetson John

I want to set a session timeout after log in using passportjs. How to set the maxAge for the session using passportjs. What is the default max age for the session that passportjs provide?

我想在使用passportjs登录后设置会话超时。如何使用passportjs为会话设置maxAge。passportjs 提供的会话的默认最大年龄是多少?

回答by

That is handled via Connect's session middleware, so for example:

这是通过 Connect 的会话中间件处理的,例如:

.use(connect.session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))

The documentationhas other useful bits that are worth reading for understanding session handling.

文档还有其他有用的部分,值得阅读以了解会话处理。

回答by Bernhard

You need to set the lifetime of the session cookie for express.session like the following example:

您需要为 express.session 设置会话 cookie 的生命周期,如下例所示:

app.use(express.session({
             secret : 'your_cookie_secret',
             cookie:{_expires : 60000000}, // time im ms
             })
        ); 

For testing I recommend a shorter expires time.

对于测试,我建议使用较短的过期时间。

回答by Bob Sheehan

Error: Most middleware (like session) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.

错误:大多数中间件(如会话)不再与 Express 捆绑在一起,必须单独安装。请参阅https://github.com/senchalabs/connect#middleware

Got this using latest node toolchain... so this may not work anymore.

使用最新的节点工具链得到了这个......所以这可能不再起作用了。