antaeus.keycloak.react 2.2.3 → 2.3.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.mjs CHANGED
@@ -27,7 +27,8 @@ var defaultConfig = {
27
27
  maxRefreshTokenLifetime: 2592e3,
28
28
  // 30 days in seconds
29
29
  showLoadingIndicator: false,
30
- loadingMessage: "Refreshing session..."
30
+ loadingMessage: "Refreshing session...",
31
+ useRefreshToken: true
31
32
  },
32
33
  advanced: {
33
34
  responseType: "code",
@@ -53,6 +54,7 @@ var KeycloakConfigBuilder = class {
53
54
  const config = this.mergeWithDefaults(userConfig);
54
55
  const storageType = config.advanced.storageType || "session";
55
56
  const storage = storageType === "local" ? window.localStorage : window.sessionStorage;
57
+ const { useRefreshToken } = config.silentRenew;
56
58
  const oidcSettings = {
57
59
  authority: config.authority,
58
60
  client_id: config.clientId,
@@ -70,6 +72,9 @@ var KeycloakConfigBuilder = class {
70
72
  response_mode: "query"
71
73
  }
72
74
  };
75
+ if (useRefreshToken !== void 0) {
76
+ oidcSettings.useRefreshToken = useRefreshToken;
77
+ }
73
78
  return oidcSettings;
74
79
  }
75
80
  /**
@@ -157,6 +162,7 @@ function useTokenLifecycle(options) {
157
162
  const [isRefreshing, setIsRefreshing] = useState(false);
158
163
  const [isRefreshTokenExpired, setIsRefreshTokenExpired] = useState(false);
159
164
  const hasCheckedOnStartup = useRef(false);
165
+ const supportsRefreshTokens = config.silentRenew?.useRefreshToken !== false;
160
166
  const log = useCallback(
161
167
  (message, ...args) => {
162
168
  if (debug) {
@@ -293,24 +299,49 @@ function useTokenLifecycle(options) {
293
299
  useEffect(() => {
294
300
  const strategy = config.silentRenew?.refreshStrategy || "both";
295
301
  const shouldCheckOnStartup = strategy === "startup" || strategy === "both";
302
+ const hasRefreshToken = Boolean(auth.user?.refresh_token);
296
303
  if (!shouldCheckOnStartup || hasCheckedOnStartup.current) {
297
304
  return;
298
305
  }
299
- if (!auth.isAuthenticated || auth.isLoading) {
306
+ if (auth.isLoading) {
300
307
  return;
301
308
  }
309
+ if (!auth.isAuthenticated) {
310
+ if (!supportsRefreshTokens) {
311
+ return;
312
+ }
313
+ if (!auth.user || !hasRefreshToken) {
314
+ return;
315
+ }
316
+ }
302
317
  hasCheckedOnStartup.current = true;
303
- log("Running startup token check with strategy:", strategy);
318
+ log(
319
+ "Running startup token check with strategy:",
320
+ strategy,
321
+ "using refresh token:",
322
+ !auth.isAuthenticated && supportsRefreshTokens
323
+ );
304
324
  checkAndRefreshTokens().catch((error) => {
305
325
  log("Startup token check failed:", error);
306
326
  });
307
327
  }, [
308
328
  auth.isAuthenticated,
309
329
  auth.isLoading,
330
+ auth.user,
310
331
  config.silentRenew?.refreshStrategy,
332
+ supportsRefreshTokens,
311
333
  checkAndRefreshTokens,
312
334
  log
313
335
  ]);
336
+ useEffect(() => {
337
+ if (hasCheckedOnStartup.current) {
338
+ const hasUser = Boolean(auth.user);
339
+ const hasRefreshToken = Boolean(auth.user?.refresh_token);
340
+ if (auth.isLoading || !auth.isAuthenticated && (!supportsRefreshTokens || !hasUser || !hasRefreshToken)) {
341
+ hasCheckedOnStartup.current = false;
342
+ }
343
+ }
344
+ }, [auth.isAuthenticated, auth.isLoading, auth.user, supportsRefreshTokens]);
314
345
  useEffect(() => {
315
346
  if (auth.user && !auth.isLoading) {
316
347
  storeFirstLoginTime();