diff --git a/packages/sdks/web-js-sdk/src/constants.ts b/packages/sdks/web-js-sdk/src/constants.ts index 697f17860..1fc110e7b 100644 --- a/packages/sdks/web-js-sdk/src/constants.ts +++ b/packages/sdks/web-js-sdk/src/constants.ts @@ -4,3 +4,6 @@ export const IS_BROWSER = typeof window !== 'undefined'; // Maximum timeout value for setTimeout // For more information, refer to https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#maximum_delay_value export const MAX_TIMEOUT = Math.pow(2, 31) - 1; + +// The amount of time (ms) to trigger the refresh before session expires +export const REFRESH_THRESHOLD = 20 * 1000; // 20 sec diff --git a/packages/sdks/web-js-sdk/src/enhancers/withAutoRefresh/helpers.ts b/packages/sdks/web-js-sdk/src/enhancers/withAutoRefresh/helpers.ts index 93d7e0b7f..0908480e5 100644 --- a/packages/sdks/web-js-sdk/src/enhancers/withAutoRefresh/helpers.ts +++ b/packages/sdks/web-js-sdk/src/enhancers/withAutoRefresh/helpers.ts @@ -1,9 +1,6 @@ import { jwtDecode, JwtPayload } from 'jwt-decode'; import logger from '../helpers/logger'; -import { MAX_TIMEOUT } from '../../constants'; - -// The amount of time (ms) to trigger the refresh before session expires -const REFRESH_THRESHOLD = 20 * 1000; // 20 sec +import { MAX_TIMEOUT, REFRESH_THRESHOLD } from '../../constants'; /** * Get the JWT expiration WITHOUT VALIDATING the JWT 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 e284af6ee..6dfe6a747 100644 --- a/packages/sdks/web-js-sdk/src/enhancers/withAutoRefresh/index.ts +++ b/packages/sdks/web-js-sdk/src/enhancers/withAutoRefresh/index.ts @@ -9,7 +9,7 @@ import { } from './helpers'; import { AutoRefreshOptions } from './types'; import logger from '../helpers/logger'; -import { IS_BROWSER } from '../../constants'; +import { IS_BROWSER, REFRESH_THRESHOLD } from '../../constants'; import { getRefreshToken } from '../withPersistTokens/helpers'; /** @@ -34,6 +34,7 @@ export const withAutoRefresh = // tab becomes visible and the session is expired, do a refresh if ( document.visibilityState === 'visible' && + sessionExpiration && new Date() > sessionExpiration ) { logger.debug('Expiration time passed, refreshing session'); @@ -51,6 +52,7 @@ export const withAutoRefresh = // if we got 401 we want to cancel all timers if (res?.status === 401) { logger.debug('Received 401, canceling all timers'); + sessionExpiration = null; clearAllTimers(); } else if (sessionJwt) { sessionExpiration = getTokenExpiration(sessionJwt); @@ -63,6 +65,13 @@ export const withAutoRefresh = clearAllTimers(); + if (timeout <= REFRESH_THRESHOLD) { + logger.debug( + 'Session is too close to expiration, not setting refresh timer', + ); + return; + } + const refreshTimeStr = new Date( Date.now() + timeout, ).toLocaleTimeString('en-US', { hour12: false });