Skip to content

Commit 57f8283

Browse files
committed
refactor: refactored shouldUpdateSessionExpiry() logic | docs: added documentation for shouldUpdateSessionExpiry
1 parent da99d51 commit 57f8283

File tree

5 files changed

+20
-61
lines changed

5 files changed

+20
-61
lines changed

spec/Auth.spec.js

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -262,56 +262,22 @@ describe('Auth', () => {
262262
});
263263

264264
describe('extendSessionOnUse', () => {
265-
const tests = [
266-
{
267-
title: 'Updated more than 24 hrs ago',
268-
sessionLength: 86460,
269-
sessionUpdatedAt: 86410,
270-
result: true,
271-
},
272-
{
273-
title: 'Updated less than 24 hrs ago',
274-
sessionLength: 86460,
275-
sessionUpdatedAt: 86390,
276-
result: false,
277-
},
278-
{
279-
title: 'Update more than an hour ago',
280-
sessionLength: 3660,
281-
sessionUpdatedAt: 3610,
282-
result: true,
283-
},
284-
{
285-
title: 'Updated less than an hour ago',
286-
sessionLength: 3660,
287-
sessionUpdatedAt: 3590,
288-
result: false,
289-
},
290-
{
291-
title: 'Updated more than a minute ago.',
292-
sessionLength: 120,
293-
sessionUpdatedAt: 70,
294-
result: true,
295-
},
296-
{
297-
title: 'Updated less than a minute ago.',
298-
sessionLength: 120,
299-
sessionUpdatedAt: 50,
300-
result: false,
301-
},
302-
];
265+
it(`shouldUpdateSessionExpiry()`, async () => {
266+
const { shouldUpdateSessionExpiry } = require('../lib/Auth');
267+
let update = new Date(Date.now() - 86410 * 1000);
303268

304-
tests.forEach(({ title, sessionLength, sessionUpdatedAt, result }) => {
305-
it(`shouldUpdateSessionExpiry() when ${title}`, async () => {
306-
const { shouldUpdateSessionExpiry } = require('../lib/Auth');
307-
const update = new Date();
308-
update.setTime(update.getTime() - sessionUpdatedAt * 1000);
309-
const res = shouldUpdateSessionExpiry(
310-
{ sessionLength: sessionLength },
311-
{ updatedAt: update }
312-
);
269+
const res = shouldUpdateSessionExpiry(
270+
{ sessionLength: 86460 },
271+
{ updatedAt: update }
272+
);
313273

314-
expect(res).toBe(result);
315-
});
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);
316282
});
317283
});

src/Auth.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,7 @@ function nobody(config) {
7171
* Checks whether session should be updated based on last update time & session length.
7272
*/
7373
function shouldUpdateSessionExpiry(config, session) {
74-
let resetAfter = 60;
75-
if (config.sessionLength > 86400) {
76-
resetAfter = 86400; // Every 24 hours
77-
} else if (config.sessionLength > 3600) {
78-
resetAfter = 3600; // 1 hour
79-
} else {
80-
resetAfter = 60; // Every minute
81-
}
82-
74+
const resetAfter = config.sessionLength / 2;
8375
const lastUpdated = new Date(session?.updatedAt);
8476
const skipRange = new Date();
8577
skipRange.setTime(skipRange.getTime() - resetAfter * 1000);

src/Options/Definitions.js

Lines changed: 2 additions & 1 deletion
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. The session will be extended when a request is received after half of the session's lifetime has passed.",
263264
action: parsers.booleanParser,
264265
default: false,
265266
},

src/Options/docs.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Options/index.js

Lines changed: 1 addition & 1 deletion
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. The session will be extended when a request is received after half of the 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)