Skip to content

Commit

Permalink
prolong session TTL on each request
Browse files Browse the repository at this point in the history
  • Loading branch information
catamphetamine committed Jan 25, 2016
1 parent fae7509 commit c7c13fc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
26 changes: 23 additions & 3 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ module.exports = function (options) {
* after everything done, refresh the session
* if session === null; delete it from store
* if session is modified, update cookie and store
* if session is not modified, update cookie maxAge and store TTL
*/
function *refreshSession (session, originalHash, isNew) {
//delete session
Expand All @@ -232,13 +233,32 @@ module.exports = function (options) {
}

// rolling session will always reset cookie and session
if (!options.rolling && newHash === originalHash) {
return debug('session not modified');
if (options.rolling) {
return;
}

debug('session modified');
compatMaxage(session.cookie);

if (newHash === originalHash) {
debug('session not modified');

// prolong session: update session TTL and session cookie maxAge
try {
if (store.bump) {
yield store.bump(this.sessionId);
}
sessionIdStore.set.call(this, this.sessionId, session);
debug('session prolonged');
} catch (err) {
debug('prolong session error: ', err.message);
errorHandler(err, 'prolong', this);
}

return;
}

debug('session modified');

// custom before save hook
beforeSave(this, session);

Expand Down
36 changes: 24 additions & 12 deletions lib/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,8 @@ Store.prototype.get = function *(sid) {
};

Store.prototype.set = function *(sid, sess) {
var ttl = this.options.ttl;
if (!ttl) {
var maxage = sess.cookie && sess.cookie.maxage;
if (typeof maxage === 'number') {
ttl = maxage;
}
// if has cookie.expires, ignore cookie.maxage
if (sess.cookie && sess.cookie.expires) {
ttl = Math.ceil(sess.cookie.expires.getTime() - Date.now());
}
}

sid = this.options.prefix + sid;
var ttl = this.ttl();
debug('SET key: %s, value: %s, ttl: %d', sid, sess, ttl);
yield this.client.set(sid, sess, ttl);
debug('SET complete');
Expand All @@ -80,4 +69,27 @@ Store.prototype.destroy = function *(sid) {
debug('DEL %s complete', sid);
};

Store.prototype.bump = function *(sid) {
sid = this.options.prefix + sid;
var ttl = this.ttl();
debug('BUMP %s, ttl: %d', sid, ttl);
yield this.client.bump(sid, ttl);
debug('BUMP complete');
};

Store.prototype.ttl = function () {
var ttl = this.options.ttl;
if (!ttl) {
var maxage = sess.cookie && sess.cookie.maxage;
if (typeof maxage === 'number') {
ttl = maxage;
}
// if has cookie.expires, ignore cookie.maxage
if (sess.cookie && sess.cookie.expires) {
ttl = Math.ceil(sess.cookie.expires.getTime() - Date.now());
}
}
return ttl;
}

module.exports = Store;

0 comments on commit c7c13fc

Please sign in to comment.