Skip to content

Commit 0a054e6

Browse files
fix: Parse Server option extendSessionOnUse not working for session lengths < 24 hours (#9113)
1 parent d8ebdb3 commit 0a054e6

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

spec/Auth.spec.js

+21
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,24 @@ describe('Auth', () => {
260260
});
261261
});
262262
});
263+
264+
describe('extendSessionOnUse', () => {
265+
it(`shouldUpdateSessionExpiry()`, async () => {
266+
const { shouldUpdateSessionExpiry } = require('../lib/Auth');
267+
let update = new Date(Date.now() - 86410 * 1000);
268+
269+
const res = shouldUpdateSessionExpiry(
270+
{ sessionLength: 86460 },
271+
{ updatedAt: update }
272+
);
273+
274+
update = new Date(Date.now() - 43210 * 1000);
275+
const res2 = shouldUpdateSessionExpiry(
276+
{ sessionLength: 86460 },
277+
{ updatedAt: update }
278+
);
279+
280+
expect(res).toBe(true);
281+
expect(res2).toBe(false);
282+
});
283+
});

src/Auth.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ function nobody(config) {
6767
return new Auth({ config, isMaster: false });
6868
}
6969

70+
/**
71+
* Checks whether session should be updated based on last update time & session length.
72+
*/
73+
function shouldUpdateSessionExpiry(config, session) {
74+
const resetAfter = config.sessionLength / 2;
75+
const lastUpdated = new Date(session?.updatedAt);
76+
const skipRange = new Date();
77+
skipRange.setTime(skipRange.getTime() - resetAfter * 1000);
78+
return lastUpdated <= skipRange;
79+
}
80+
7081
const throttle = {};
7182
const renewSessionIfNeeded = async ({ config, session, sessionToken }) => {
7283
if (!config?.extendSessionOnUse) {
@@ -88,10 +99,7 @@ const renewSessionIfNeeded = async ({ config, session, sessionToken }) => {
8899
const { results } = await query.execute();
89100
session = results[0];
90101
}
91-
const lastUpdated = new Date(session?.updatedAt);
92-
const yesterday = new Date();
93-
yesterday.setDate(yesterday.getDate() - 1);
94-
if (lastUpdated > yesterday || !session) {
102+
if (!shouldUpdateSessionExpiry(config, session) || !session) {
95103
return;
96104
}
97105
const expiresAt = config.generateSessionExpiresAt();
@@ -579,6 +587,7 @@ module.exports = {
579587
maintenance,
580588
nobody,
581589
readOnly,
590+
shouldUpdateSessionExpiry,
582591
getAuthForSessionToken,
583592
getAuthForLegacySessionToken,
584593
findUsersWithAuthData,

src/Options/Definitions.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ module.exports.ParseServerOptions = {
259259
},
260260
extendSessionOnUse: {
261261
env: 'PARSE_SERVER_EXTEND_SESSION_ON_USE',
262-
help: 'Whether Parse Server should automatically extend a valid session by the sessionLength',
262+
help:
263+
"Whether Parse Server should automatically extend a valid session by the sessionLength. In order to reduce the number of session updates in the database, a session will only be extended when a request is received after at least half of the current session's lifetime has passed.",
263264
action: parsers.booleanParser,
264265
default: false,
265266
},

src/Options/docs.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Options/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export interface ParseServerOptions {
228228
/* Session duration, in seconds, defaults to 1 year
229229
:DEFAULT: 31536000 */
230230
sessionLength: ?number;
231-
/* Whether Parse Server should automatically extend a valid session by the sessionLength
231+
/* Whether Parse Server should automatically extend a valid session by the sessionLength. In order to reduce the number of session updates in the database, a session will only be extended when a request is received after at least half of the current session's lifetime has passed.
232232
:DEFAULT: false */
233233
extendSessionOnUse: ?boolean;
234234
/* Default value for limit option on queries, defaults to `100`.

0 commit comments

Comments
 (0)