本文介绍了如何保持NodeJS Passport会话的生命的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用Node + Express + Passport进行身份验证,并将会话信息持久保存到Redis.我在会话Cookie上设置了maxAge,以在一小时内超时.看起来一切正常,但问题是,无论用户的活动如何,会话Cookie都会在一小时后过期.

We're Node + Express + Passport for authentication, and persisting the session info to Redis. I have maxAge set on the session cookie, to time out in one hour. That all seems to be working fine but the problem is, the session cookie will expire in one hour regardless of the user's activity.

有没有一种方法可以手动刷新/保持会话cookie?

Is there a way I can manually refresh/keep alive the session cookie?

推荐答案

您可能需要在会话中使用滚动"选项.这在每个响应中强制设置cookie"并重置到期日期".您要将滚动设置为true.

You'll likely want to use the "rolling" option for your session. This "forces a cookie set on every response" and "resets the expiration date." You want to set rolling to true.

还请注意重新保存"选项.那即使没有修改也强制保存会话..."您可能还希望将该选项设置为true.请注意,即使此选项的默认值为true,也应显式设置该值.现在不建议使用此选项的默认值,而不必显式设置它.

Also pay attention to the "resave" option. That "forces session to be saved even when unmodified..." You'll likely want to set that option to true as well. Note that even though the default value is true for this option, you should set the value explicitly. Relying on the default for this option, rather than setting it explicitly, is now deprecated.

尝试类似的东西:

app.use( session( { secret: 'keyboard cat',
                    cookie: { maxAge: 60000 },
                    rolling: true,
                    resave: true,
                    saveUninitialized: false
                  }
         )
);

这是文档.在选项"和"options.resave"下查看: https://github.com/expressjs/session .

Here's the documentation. Look under "Options" and "options.resave": https://github.com/expressjs/session .

这篇关于如何保持NodeJS Passport会话的生命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 17:39