Skip to content

Commit 4c98996

Browse files
Debounce token refetches on tab focus + network reconnect
1 parent f207ad7 commit 4c98996

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "git",
66
"url": "https://github.com/PropelAuth/javascript"
77
},
8-
"version": "1.2.3",
8+
"version": "1.2.4",
99
"keywords": [
1010
"auth",
1111
"user",

src/client.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { AuthenticationInfo, fetchAuthenticationInfo, logout } from "./api"
2-
import { currentTimeSeconds, getLocalStorageNumber, hasLocalStorage, hasWindow } from "./helpers"
1+
import {AuthenticationInfo, fetchAuthenticationInfo, logout} from "./api"
2+
import {currentTimeSeconds, getLocalStorageNumber, hasLocalStorage, hasWindow} from "./helpers"
33

44
const LOGGED_IN_AT_KEY = "__PROPEL_AUTH_LOGGED_IN_AT"
55
const LOGGED_OUT_AT_KEY = "__PROPEL_AUTH_LOGGED_OUT_AT"
6-
const STALE_AUTH_INFO_THRESHOLD_SECS = 4 * 60
6+
const AUTH_TOKEN_REFRESH_BEFORE_EXPIRATION_SECONDS = 4 * 60
7+
const DEBOUNCE_DURATION_FOR_REFOCUS_SECONDS = 4 * 60
78

89
export interface IAuthClient {
910
/**
@@ -88,6 +89,7 @@ interface ClientState {
8889
lastLoggedInAtMessage: number | null
8990
lastLoggedOutAtMessage: number | null
9091
refreshInterval: number | null
92+
lastRefresh: number | null
9193
readonly authUrl: string
9294
}
9395

@@ -118,6 +120,7 @@ export function createClient(authOptions: IAuthOptions): IAuthClient {
118120
lastLoggedOutAtMessage: getLocalStorageNumber(LOGGED_OUT_AT_KEY),
119121
authUrl: authOptions.authUrl,
120122
refreshInterval: null,
123+
lastRefresh: null,
121124
}
122125

123126
// Helper functions
@@ -165,6 +168,7 @@ export function createClient(authOptions: IAuthOptions): IAuthClient {
165168
updateLastLoggedInAt()
166169
}
167170

171+
clientState.lastRefresh = currentTimeSeconds()
168172
clientState.initialLoadFinished = true
169173
}
170174

@@ -214,7 +218,7 @@ export function createClient(authOptions: IAuthOptions): IAuthClient {
214218
} else if (!clientState.authenticationInfo) {
215219
return await forceRefreshToken(false)
216220
} else if (
217-
currentTimeSecs + STALE_AUTH_INFO_THRESHOLD_SECS >
221+
currentTimeSecs + AUTH_TOKEN_REFRESH_BEFORE_EXPIRATION_SECONDS >
218222
clientState.authenticationInfo.expiresAtSeconds
219223
) {
220224
// Small edge case: If we were being proactive
@@ -295,8 +299,13 @@ export function createClient(authOptions: IAuthOptions): IAuthClient {
295299
}
296300

297301
// If we were offline or on a different tab, when we return, refetch auth info
302+
// Some browsers trigger focus more often than we'd like, so we'll debounce a little here as well
298303
const onOnlineOrFocus = async function () {
299-
await forceRefreshToken(true)
304+
if (clientState.lastRefresh && currentTimeSeconds() > clientState.lastRefresh + DEBOUNCE_DURATION_FOR_REFOCUS_SECONDS) {
305+
await forceRefreshToken(true)
306+
} else {
307+
await client.getAuthenticationInfoOrNull()
308+
}
300309
}
301310

302311
if (hasWindow()) {

0 commit comments

Comments
 (0)