From 897fe1a9d7d8c9eec3182e17dd127ed7981e9786 Mon Sep 17 00:00:00 2001 From: Asaf Shen Date: Mon, 13 Jan 2025 16:48:02 +0200 Subject: [PATCH] chore: refactor timeout code --- .../src/enhancers/withAutoRefresh/index.ts | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/packages/sdks/web-js-sdk/src/enhancers/withAutoRefresh/index.ts b/packages/sdks/web-js-sdk/src/enhancers/withAutoRefresh/index.ts index ae5196787..e284af6ee 100644 --- a/packages/sdks/web-js-sdk/src/enhancers/withAutoRefresh/index.ts +++ b/packages/sdks/web-js-sdk/src/enhancers/withAutoRefresh/index.ts @@ -5,16 +5,13 @@ import { addHooks, getAuthInfoFromResponse } from '../helpers'; import { createTimerFunctions, getTokenExpiration, - millisecondsUntilDate, + getAutoRefreshTimeout, } from './helpers'; import { AutoRefreshOptions } from './types'; import logger from '../helpers/logger'; -import { IS_BROWSER, MAX_TIMEOUT } from '../../constants'; +import { IS_BROWSER } from '../../constants'; import { getRefreshToken } from '../withPersistTokens/helpers'; -// The amount of time (ms) to trigger the refresh before session expires -const REFRESH_THRESHOLD = 20 * 1000; // 20 sec - /** * Automatically refresh the session token before it expires * It uses the the refresh token that is extracted from API response to do that @@ -62,15 +59,8 @@ export const withAutoRefresh = return; } refreshToken = refreshJwt; - let timeout = - millisecondsUntilDate(sessionExpiration) - REFRESH_THRESHOLD; + const timeout = getAutoRefreshTimeout(sessionExpiration); - if (timeout > MAX_TIMEOUT) { - logger.debug( - `Timeout is too large (${timeout}ms), setting it to ${MAX_TIMEOUT}ms`, - ); - timeout = MAX_TIMEOUT; - } clearAllTimers(); const refreshTimeStr = new Date(