antaeus.keycloak.react 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -152,6 +152,14 @@ var useAuth = reactOidcContext.useAuth;
152
152
 
153
153
  // src/hooks/useTokenLifecycle.ts
154
154
  var STORAGE_KEY_PREFIX = "keycloak_first_login_";
155
+ var isNavigatorOffline = () => typeof navigator !== "undefined" && "onLine" in navigator && navigator.onLine === false;
156
+ var isTransientNetworkError = (error) => {
157
+ const message = error.message?.toLowerCase?.() ?? "";
158
+ if (!message) {
159
+ return false;
160
+ }
161
+ return message.includes("network") || message.includes("failed to fetch") || message.includes("fetch") || message.includes("timeout") || message.includes("temporarily unavailable") || message.includes("dns") || message.includes("offline");
162
+ };
155
163
  function useTokenLifecycle(options) {
156
164
  const {
157
165
  config,
@@ -166,6 +174,11 @@ function useTokenLifecycle(options) {
166
174
  const [isRefreshTokenExpired, setIsRefreshTokenExpired] = react.useState(false);
167
175
  const hasCheckedOnStartup = react.useRef(false);
168
176
  const supportsRefreshTokens = config.silentRenew?.useRefreshToken !== false;
177
+ const [pendingRetry, setPendingRetry] = react.useState(null);
178
+ const deferredRefreshReasonRef = react.useRef(null);
179
+ const enqueueRetry = react.useCallback((reason) => {
180
+ setPendingRetry((current) => current === reason ? current : reason);
181
+ }, []);
169
182
  const log = react.useCallback(
170
183
  (message, ...args) => {
171
184
  if (debug) {
@@ -216,6 +229,7 @@ function useTokenLifecycle(options) {
216
229
  return;
217
230
  }
218
231
  setIsRefreshing(true);
232
+ deferredRefreshReasonRef.current = null;
219
233
  try {
220
234
  if (checkRefreshTokenLifetime()) {
221
235
  log("Refresh token exceeded max lifetime, cannot refresh");
@@ -223,6 +237,12 @@ function useTokenLifecycle(options) {
223
237
  onRefreshTokenExpired?.();
224
238
  return;
225
239
  }
240
+ if (isNavigatorOffline()) {
241
+ log("Device offline, deferring token refresh until connection is restored");
242
+ deferredRefreshReasonRef.current = "offline";
243
+ enqueueRetry("offline");
244
+ return;
245
+ }
226
246
  onRefreshStart?.();
227
247
  log("Starting token refresh");
228
248
  await auth.signinSilent();
@@ -230,6 +250,21 @@ function useTokenLifecycle(options) {
230
250
  onRefreshSuccess?.();
231
251
  } catch (error) {
232
252
  const err = error instanceof Error ? error : new Error(String(error));
253
+ if (isTransientNetworkError(err)) {
254
+ const reason = isNavigatorOffline() ? "offline" : "network";
255
+ deferredRefreshReasonRef.current = reason;
256
+ if (reason === "offline") {
257
+ log("Token refresh deferred because device is offline");
258
+ } else {
259
+ log(
260
+ "Token refresh failed due to transient network issue, will retry automatically:",
261
+ err.message
262
+ );
263
+ }
264
+ enqueueRetry(reason);
265
+ onRefreshError?.(err);
266
+ return;
267
+ }
233
268
  log("Token refresh failed:", err.message);
234
269
  onRefreshError?.(err);
235
270
  throw err;
@@ -244,6 +279,7 @@ function useTokenLifecycle(options) {
244
279
  onRefreshSuccess,
245
280
  onRefreshError,
246
281
  onRefreshTokenExpired,
282
+ enqueueRetry,
247
283
  log
248
284
  ]);
249
285
  const checkAndRefreshTokens = react.useCallback(async () => {
@@ -282,6 +318,14 @@ function useTokenLifecycle(options) {
282
318
  if (timeUntilExpiry <= threshold) {
283
319
  try {
284
320
  await refreshToken();
321
+ if (deferredRefreshReasonRef.current) {
322
+ log(
323
+ "Token refresh was deferred due to pending retry:",
324
+ deferredRefreshReasonRef.current
325
+ );
326
+ deferredRefreshReasonRef.current = null;
327
+ return false;
328
+ }
285
329
  return true;
286
330
  } catch (error) {
287
331
  log("Failed to refresh tokens:", error);
@@ -299,6 +343,55 @@ function useTokenLifecycle(options) {
299
343
  getStorageKey,
300
344
  log
301
345
  ]);
346
+ react.useEffect(() => {
347
+ if (pendingRetry === null) {
348
+ return;
349
+ }
350
+ if (typeof window === "undefined") {
351
+ return;
352
+ }
353
+ let cancelled = false;
354
+ const executeRetry = () => {
355
+ if (cancelled) {
356
+ return;
357
+ }
358
+ if (isRefreshing) {
359
+ log("Skipping scheduled token refresh retry because a refresh is already in progress");
360
+ return;
361
+ }
362
+ refreshToken().catch((err) => {
363
+ log("Scheduled token refresh retry failed:", err);
364
+ });
365
+ };
366
+ if (pendingRetry === "offline") {
367
+ log("Waiting for online event to retry token refresh");
368
+ const handleOnline = () => {
369
+ if (cancelled) {
370
+ return;
371
+ }
372
+ log("Online event detected, retrying deferred token refresh");
373
+ setPendingRetry(null);
374
+ executeRetry();
375
+ };
376
+ window.addEventListener("online", handleOnline, { once: true });
377
+ return () => {
378
+ cancelled = true;
379
+ window.removeEventListener("online", handleOnline);
380
+ };
381
+ }
382
+ const timeoutId = window.setTimeout(() => {
383
+ if (cancelled) {
384
+ return;
385
+ }
386
+ log("Retrying token refresh after transient network failure delay");
387
+ setPendingRetry(null);
388
+ executeRetry();
389
+ }, 5e3);
390
+ return () => {
391
+ cancelled = true;
392
+ window.clearTimeout(timeoutId);
393
+ };
394
+ }, [pendingRetry, refreshToken, isRefreshing, log]);
302
395
  react.useEffect(() => {
303
396
  const strategy = config.silentRenew?.refreshStrategy || "both";
304
397
  const shouldCheckOnStartup = strategy === "startup" || strategy === "both";