Skip to content

Commit ca86ab3

Browse files
authored
Invert the TTL value and extend to fixed (#1480)
* Now this is giving predictable results so far. Refs: * https://github.com/jdesboeufs/connect-mongo/blob/9f86f90/README.md#session-expiration * https://github.com/jdesboeufs/connect-mongo/blob/9f86f90/README.md#remove-expired-sessions NOTES: These versions of the docs are ambiguous. The TTL is how often it reaches out to the DB... if it finds an expired session **or** a session cookie it will then remove it. So the inversion of the logic should fix this as it checks every 10ish minutes for expiry as well as session cookies and destroys them. Had a light bulb flip on with `interval` being doc'd at 10 minutes so I thought to try TTL at that... and voila. :) This theoretically should solve #604 by sticking to this methodology *(until MongoDB changes something heh)* When available will do some code condensing to reuse some more code if applicable. Post #1471 ... related to #604 Auto-merge
1 parent ca8847c commit ca86ab3

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ process.on('SIGINT', function () {
142142
var sessionStore = new MongoStore({
143143
mongooseConnection: db,
144144
autoRemove: 'native',
145-
ttl: (6 * 3) * 60 * 60 // hours ; 14 * 24 * 60 * 60 = 14 days. Default
145+
ttl: 10 * 60 // seconds to minutes ; 14 * 24 * 60 * 60 = 14 days. Default
146146
});
147147

148148
// See https://hacks.mozilla.org/2013/01/building-a-node-js-server-that-wont-melt-a-node-js-holiday-season-part-5/

controllers/user.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,15 @@ exports.userEditPreferencesPage = function (aReq, aRes, aNext) {
10311031

10321032
// User session control
10331033
tasks.push(function (aCallback) {
1034-
if (aReq.session.cookie.expires) {
1034+
if (!aReq.session.passport) {
1035+
aReq.session.passport = {};
1036+
}
1037+
1038+
if (!aReq.session.passport.oujsOptions) {
1039+
aReq.session.passport.oujsOptions = {};
1040+
}
1041+
1042+
if (!aReq.session.passport.oujsOptions.extended) {
10351043
options.sessionControl = true;
10361044
}
10371045

libs/modifySessions.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,30 @@ exports.expand = function (aReq, aUser, aCallback) {
9696

9797
// Extend a single session
9898
exports.extend = function (aReq, aUser, aCallback) {
99+
var expiry = moment(aReq.session.cookie.expires);
100+
99101
if (!aUser) {
100102
aCallback('No User');
101103
return;
102104
}
103105

104-
if (!aReq.session.cookie.expires) {
106+
if (!aReq.session.passport) {
107+
aReq.session.passport = {};
108+
}
109+
110+
if (!aReq.session.passport.oujsOptions) {
111+
aReq.session.passport.oujsOptions = {};
112+
}
113+
114+
if (aReq.session.passport.oujsOptions.extended) {
105115
aCallback('Already extended');
106116
return;
107117
}
108118

109-
// NOTE: Currently allow on any session with
110-
// no additional User restrictions yet...
119+
expiry = expiry.add(6 * 2, 'h'); // NOTE: Keep this addition to expanded timeout in sync with app.js
120+
aReq.session.passport.oujsOptions.extended = true;
111121

112-
aReq.session.cookie.expires = false;
122+
aReq.session.cookie.expires = expiry.toDate();
113123
aReq.session.save(aCallback);
114124
};
115125

views/includes/session.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
{{^cookie.sameSite}}<i class="fa fa-circle-o"></i>{{/cookie.sameSite}}
2525
</span>
2626
<span class="label label-{{#cookie.originalMaxAge}}success{{/cookie.originalMaxAge}}{{^cookie.originalMaxAge}}warning{{/cookie.originalMaxAge}}" title="originalMaxAge">
27-
{{#cookie.originalMaxAge}}{{cookie.originalMaxAgeHumanized}}{{/cookie.originalMaxAge}}{{^cookie.originalMaxAge}}&and;{{/cookie.originalMaxAge}}
27+
{{#cookie.originalMaxAge}}{{#passport.oujsOptions.extended}}&and;{{/passport.oujsOptions.extended}}{{cookie.originalMaxAgeHumanized}}{{/cookie.originalMaxAge}}{{^cookie.originalMaxAge}}&and;{{/cookie.originalMaxAge}}
2828
</span>
2929
{{#passport.oujsOptions.remoteAddress}}
3030
<span class="label label-info" title="remoteAddress">{{passport.oujsOptions.remoteAddressMask}}</span>

0 commit comments

Comments
 (0)