Sails.js rolling sessions

后端 未结 1 1378
[愿得一人]
[愿得一人] 2021-01-06 11:44

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 par

相关标签:
1条回答
  • 2021-01-06 12:22

    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'
        ]
    }
    
    0 讨论(0)
提交回复
热议问题