Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle close timeouts #887

Draft
wants to merge 5 commits into
base: chore-refactor-timeout-code
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/sdks/web-js-sdk/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 10 additions & 1 deletion packages/sdks/web-js-sdk/src/enhancers/withAutoRefresh/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -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 });
Expand Down
Loading