Sails.js滚动会话 [英] Sails.js rolling sessions

查看:88
本文介绍了Sails.js滚动会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

滚动会话是在没有用户活动(不包括websockets实时更新数据)的情况下在设定的时间内到期的会话。如果用户在到期前访问网站的其他部分,则应延长到期日期。我如何使用Sails.js做到这一点?在<$ c $中的 cookie 下设置 maxAge 到期 c> /config/session.js 没有达到预期的效果。到期时不会因另一个页面加载而延长。它保持不变。

A rolling session is a session that expires in a set amount of time should there be no user activity(excluding websockets live updating data). If the user visits another part of the site before expiry, the expiry should then be extended. How would I do this with Sails.js? Setting maxAge and expires under cookie in /config/session.js does not have the desired effect. The expiry does not get extended with another page load. It stays constant.

编辑:一旦Sails.js升级其Express版本,这会得到解决吗?我看到 https://github.com/expressjs/session 滚动选项。

Will this be resolved once Sails.js upgrades its Express version? I see https://github.com/expressjs/session has a rolling option.

EDIT2:我看到了这个答案: ExpressJS会话到期尽管活动我是否需要复制和粘贴

I see this answer: ExpressJS session expiring despite activity Would I need to copy and paste

req.session._garbage = Date();
req.session.touch();

到控制器中的每条路线上?有更好的方法吗?

onto each route in the controller? Is there a better way?

推荐答案

在Sails中更改Express依赖关系并不是我们可以轻视的。但与此同时,您可以通过多种方式处理此问题,具体取决于您要触发cookie刷新的条件:

Changing the Express dependency in Sails is not something we take lightly. But in the meantime, you can handle this in a couple of ways, depending on the conditions you'd like to trigger the cookie refresh:


  • 如果您只需要在运行控制器操作时进行刷新,则可以将代码放在全局策略。这不适用于直接映射到视图或静态资产的路由。

  • If you only need the refresh to happen when a controller action is run, you can put your code in a global policy. This won't apply to routes that are mapped directly to views, or to static assets.

config / policies:

'*': 'refreshSessionCookie'

api / policies / refreshSessionCookie:

module.exports = function(req, res, next) {
    req.session._garbage = Date();
    req.session.touch();
    return next();
}




  • 如果您希望随时进行刷新具有会话的用户请求任何,无论是控制器,视图还是静态资产,您都可以将代码放在自定义中间件将针对每个请求运行。

    • If you want the refresh to happen any time a user with a session makes a request for anything, be it a controller, view or static asset, you can put the code in custom middleware that will run for every request.
    • config / http.js:

      middleware: {
      
          refreshSessionCookie: function(req, res, next) {
              req.session._garbage = Date();
              req.session.touch();
              return next();
          },
      
          order: [
            'startRequestTimer',
            'cookieParser',
            'session',
            'refreshSessionCookie', // <-- your custom middleware
            'bodyParser',
            'handleBodyParserError',
            'compress',
            'methodOverride',
            'poweredBy',
            '$custom',
            'router',
            'www',
            'favicon',
            '404',
            '500'
          ]
      }
      

      这篇关于Sails.js滚动会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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