Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,24 @@ set to `false`, the cookie will not be set on a response with an uninitialized
session. This option only modifies the behavior when an existing session was
loaded for the request.

**Note** If this is set to true, option `rollingFunction` is ignored and every request will roll the session.

##### rollingFunction

Similar to [`rolling`](#rolling) option, but can conditionally roll the session. Only if the provided function returns true, the response will contain the cookie with the reset expiration to the original [`maxAge`](#cookiemaxage).

```js
/**
* @param {Object} req - Request object from the middleware.
* @returns {Boolean} - Whether the session will be rolled or not.
* Use req.session to access the session object.
*/
...
rollingFunction: function([req]): Boolean {...}
```

**Note** In order for this function to be called, [`rolling`](#rolling) must be `false`.

##### saveUninitialized

Forces a session that is "uninitialized" to be saved to the store. A session is
Expand Down
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ var defer = typeof setImmediate === 'function'
* @param {Boolean} [options.proxy]
* @param {Boolean} [options.resave] Resave unmodified sessions back to the store
* @param {Boolean} [options.rolling] Enable/disable rolling session expiration
* @param {Function} [options.rollingFunction] Function for enabling/disabling rolling session expiration
* @param {Boolean} [options.saveUninitialized] Save uninitialized sessions to the store
* @param {String|Array} [options.secret] Secret for signing session ID
* @param {Object} [options.store=MemoryStore] Session store
Expand Down Expand Up @@ -108,6 +109,9 @@ function session(options) {
// get the rolling session option
var rollingSessions = Boolean(opts.rolling)

// get the rolling function option
var rollingFunction = opts.rollingFunction;

// get the save uninitialized session option
var saveUninitializedSession = opts.saveUninitialized

Expand Down Expand Up @@ -456,6 +460,14 @@ function session(options) {
return false;
}

if (
!rollingSessions &&
cookieId === req.sessionID &&
typeof rollingFunction === "function"
) {
return rollingFunction(req);
}

return cookieId !== req.sessionID
? saveUninitializedSession || isModified(req.session)
: rollingSessions || req.session.cookie.expires != null && isModified(req.session);
Expand Down
106 changes: 106 additions & 0 deletions test/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,112 @@ describe('session()', function(){
});
});

it('should not force cookie if rolling function returns false', function(done){
var server = createServer(
{
rollingFunction: function() {
return false;
}
},
function(req, res) {
req.session.user = "bob";
res.end();
}
);

request(server)
.get('/')
.expect(shouldSetCookie('connect.sid'))
.expect(200, function(err, res){
if (err) return done(err);
request(server)
.get('/')
.set('Cookie', cookie(res))
.expect(shouldNotHaveHeader('Set-Cookie'))
.expect(200, done)
});
});

it('should force cookie if rolling function returns false and rolling option is set', function(done){
var server = createServer(
{
rollingFunction: function() {
return false;
},
rolling: true
},
function(req, res) {
req.session.user = "bob";
res.end();
}
);

request(server)
.get('/')
.expect(shouldSetCookie('connect.sid'))
.expect(200, function(err, res){
if (err) return done(err);
request(server)
.get('/')
.set('Cookie', cookie(res))
.expect(shouldSetCookie('connect.sid'))
.expect(200, done)
});
});

it('should force cookie if rolling function returns true and rolling option is set', function(done){
var server = createServer(
{
rollingFunction: function() {
return true;
},
rolling: true
},
function(req, res) {
req.session.user = "bob";
res.end();
}
);

request(server)
.get('/')
.expect(shouldSetCookie('connect.sid'))
.expect(200, function(err, res){
if (err) return done(err);
request(server)
.get('/')
.set('Cookie', cookie(res))
.expect(shouldSetCookie('connect.sid'))
.expect(200, done)
});
});

it('should force cookie if rolling function returns true', function(done){
var server = createServer(
{
rollingFunction: function() {
return true;
}
},
function(req, res) {
req.session.user = "bob";
res.end();
}
);

request(server)
.get('/')
.expect(shouldSetCookie('connect.sid'))
.expect(200, function(err, res){
if (err) return done(err);
request(server)
.get('/')
.set('Cookie', cookie(res))
.expect(shouldSetCookie('connect.sid'))
.expect(200, done)
});
});

it('should not force cookie on uninitialized session if saveUninitialized option is set to false', function(done){
var store = new session.MemoryStore()
var server = createServer({ store: store, rolling: true, saveUninitialized: false })
Expand Down