antaeus.keycloak.react 2.6.4 → 2.7.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/README.md CHANGED
@@ -169,6 +169,55 @@ auth.signinRedirect()
169
169
  auth.signoutRedirect()
170
170
  ```
171
171
 
172
+ ### useStoredOidcSession()
173
+
174
+ Inspect the OIDC user record stored by `oidc-client-ts` without duplicating the
175
+ library's storage-key format in an application. This is useful when an app wants
176
+ to decide whether to attempt a silent session restore or show a login prompt
177
+ immediately.
178
+
179
+ ```tsx
180
+ import { useAuth, useStoredOidcSession } from 'antaeus.keycloak.react';
181
+
182
+ function AuthGate() {
183
+ const auth = useAuth();
184
+ const storedSession = useStoredOidcSession();
185
+
186
+ if (!auth.isAuthenticated && !storedSession.hasRefreshToken) {
187
+ return <button onClick={() => auth.signinRedirect()}>Sign in</button>;
188
+ }
189
+
190
+ return <App />;
191
+ }
192
+ ```
193
+
194
+ The hook reads from the same storage configured on `KeycloakProvider` via
195
+ `advanced.storageType`. It does not trigger network calls or change provider
196
+ state.
197
+
198
+ For non-React code or tests, use the exported helpers directly:
199
+
200
+ ```ts
201
+ import {
202
+ getOidcUserStorageKey,
203
+ getStoredOidcSession,
204
+ getStoredOidcUser,
205
+ hasStoredOidcRefreshToken,
206
+ } from 'antaeus.keycloak.react';
207
+
208
+ const storageKey = getOidcUserStorageKey(authority, clientId);
209
+ const storedUser = getStoredOidcUser({ authority, clientId, storage: localStorage });
210
+ const session = getStoredOidcSession({ authority, clientId, storage: localStorage });
211
+ const canTrySilentRestore = hasStoredOidcRefreshToken({
212
+ authority,
213
+ clientId,
214
+ storage: localStorage,
215
+ });
216
+ ```
217
+
218
+ These helpers are additive APIs. They do not alter default provider behavior,
219
+ token refresh, silent renew, or session monitoring.
220
+
172
221
  ## Token Bridge (Non-React Consumers)
173
222
 
174
223
  Expose tokens to plain TypeScript modules (SignalR, fetch helpers, etc.):
package/dist/index.d.mts CHANGED
@@ -1441,6 +1441,31 @@ interface UseTokenLifecycleReturn {
1441
1441
  */
1442
1442
  declare function useTokenLifecycle(options: UseTokenLifecycleOptions): UseTokenLifecycleReturn;
1443
1443
 
1444
+ interface StoredOidcSessionUser {
1445
+ access_token?: string;
1446
+ refresh_token?: string;
1447
+ expires_at?: number;
1448
+ [key: string]: unknown;
1449
+ }
1450
+ interface StoredOidcSessionInfo {
1451
+ storageKey: string | null;
1452
+ storedUser: StoredOidcSessionUser | null;
1453
+ hasStoredUser: boolean;
1454
+ hasRefreshToken: boolean;
1455
+ }
1456
+ interface StoredOidcSessionArgs {
1457
+ authority?: string;
1458
+ clientId?: string;
1459
+ storage?: Pick<Storage, 'getItem'>;
1460
+ }
1461
+ declare const getOidcUserStorageKey: (authority?: string, clientId?: string) => string | null;
1462
+ declare const getStoredOidcUser: ({ authority, clientId, storage, }: StoredOidcSessionArgs) => StoredOidcSessionUser | null;
1463
+ declare const hasStoredOidcRefreshToken: (args: StoredOidcSessionArgs) => boolean;
1464
+ declare const getStoredOidcSession: (args: StoredOidcSessionArgs) => StoredOidcSessionInfo;
1465
+
1466
+ type UseStoredOidcSessionReturn = StoredOidcSessionInfo;
1467
+ declare const useStoredOidcSession: () => UseStoredOidcSessionReturn;
1468
+
1444
1469
  /**
1445
1470
  * Options for configuring the SignalR connection
1446
1471
  */
@@ -1721,4 +1746,4 @@ declare function pollForToken(apiBaseUrl: string, deviceCode: string): Promise<T
1721
1746
  */
1722
1747
  declare function fetchUserInfo(apiBaseUrl: string, accessToken: string): Promise<any>;
1723
1748
 
1724
- export { type AdvancedOidcOptions, CONFIG_PRESETS, type ConfigPreset, type DeviceAuthorizationResponse, type DeviceFlowError, DeviceLogin, type DeviceLoginProps, type EventCallbacks, type FeatureFlags, type KeycloakConfig, KeycloakConfigBuilder, type KeycloakDisabledReason, KeycloakProvider, type KeycloakProviderFullConfigProps, type KeycloakProviderProps, type KeycloakProviderZeroConfigProps, type KeycloakStatus, KeycloakStatusProvider, LoginButton, type LoginButtonProps, LogoutButton, type LogoutButtonProps, type PolicyContext, type PolicyFunction, ProtectedRoute, type ProtectedRouteProps, SessionMonitor, type SessionMonitorProps, type SilentRenewConfig, TokenBridge, type TokenBridgeProps, TokenBridgeSync, TokenLifecycleManager, type TokenLifecycleManagerProps, type TokenListener, type TokenResponse, type UseProtectedFetchOptions, type UseProtectedFetchReturn, type UseRolesReturn, type UseScopesReturn, type UseSignalRConnectionOptions, type UseSignalRConnectionReturn, type UseTokenLifecycleOptions, type UseTokenLifecycleReturn, UserProfile, type UserProfileProps, createTokenBridge, defaultConfig, defaultTokenBridge, fetchUserInfo, getConfigFromEnv, getConfigPreset, mergeWithPreset, pollForToken, requestDeviceCode, useAuth, useKeycloakStatus, useProtectedFetch, useRoles, useScopes, useSignalRConnection, useTokenLifecycle };
1749
+ export { type AdvancedOidcOptions, CONFIG_PRESETS, type ConfigPreset, type DeviceAuthorizationResponse, type DeviceFlowError, DeviceLogin, type DeviceLoginProps, type EventCallbacks, type FeatureFlags, type KeycloakConfig, KeycloakConfigBuilder, type KeycloakDisabledReason, KeycloakProvider, type KeycloakProviderFullConfigProps, type KeycloakProviderProps, type KeycloakProviderZeroConfigProps, type KeycloakStatus, KeycloakStatusProvider, LoginButton, type LoginButtonProps, LogoutButton, type LogoutButtonProps, type PolicyContext, type PolicyFunction, ProtectedRoute, type ProtectedRouteProps, SessionMonitor, type SessionMonitorProps, type SilentRenewConfig, type StoredOidcSessionArgs, type StoredOidcSessionInfo, type StoredOidcSessionUser, TokenBridge, type TokenBridgeProps, TokenBridgeSync, TokenLifecycleManager, type TokenLifecycleManagerProps, type TokenListener, type TokenResponse, type UseProtectedFetchOptions, type UseProtectedFetchReturn, type UseRolesReturn, type UseScopesReturn, type UseSignalRConnectionOptions, type UseSignalRConnectionReturn, type UseStoredOidcSessionReturn, type UseTokenLifecycleOptions, type UseTokenLifecycleReturn, UserProfile, type UserProfileProps, createTokenBridge, defaultConfig, defaultTokenBridge, fetchUserInfo, getConfigFromEnv, getConfigPreset, getOidcUserStorageKey, getStoredOidcSession, getStoredOidcUser, hasStoredOidcRefreshToken, mergeWithPreset, pollForToken, requestDeviceCode, useAuth, useKeycloakStatus, useProtectedFetch, useRoles, useScopes, useSignalRConnection, useStoredOidcSession, useTokenLifecycle };
package/dist/index.d.ts CHANGED
@@ -1441,6 +1441,31 @@ interface UseTokenLifecycleReturn {
1441
1441
  */
1442
1442
  declare function useTokenLifecycle(options: UseTokenLifecycleOptions): UseTokenLifecycleReturn;
1443
1443
 
1444
+ interface StoredOidcSessionUser {
1445
+ access_token?: string;
1446
+ refresh_token?: string;
1447
+ expires_at?: number;
1448
+ [key: string]: unknown;
1449
+ }
1450
+ interface StoredOidcSessionInfo {
1451
+ storageKey: string | null;
1452
+ storedUser: StoredOidcSessionUser | null;
1453
+ hasStoredUser: boolean;
1454
+ hasRefreshToken: boolean;
1455
+ }
1456
+ interface StoredOidcSessionArgs {
1457
+ authority?: string;
1458
+ clientId?: string;
1459
+ storage?: Pick<Storage, 'getItem'>;
1460
+ }
1461
+ declare const getOidcUserStorageKey: (authority?: string, clientId?: string) => string | null;
1462
+ declare const getStoredOidcUser: ({ authority, clientId, storage, }: StoredOidcSessionArgs) => StoredOidcSessionUser | null;
1463
+ declare const hasStoredOidcRefreshToken: (args: StoredOidcSessionArgs) => boolean;
1464
+ declare const getStoredOidcSession: (args: StoredOidcSessionArgs) => StoredOidcSessionInfo;
1465
+
1466
+ type UseStoredOidcSessionReturn = StoredOidcSessionInfo;
1467
+ declare const useStoredOidcSession: () => UseStoredOidcSessionReturn;
1468
+
1444
1469
  /**
1445
1470
  * Options for configuring the SignalR connection
1446
1471
  */
@@ -1721,4 +1746,4 @@ declare function pollForToken(apiBaseUrl: string, deviceCode: string): Promise<T
1721
1746
  */
1722
1747
  declare function fetchUserInfo(apiBaseUrl: string, accessToken: string): Promise<any>;
1723
1748
 
1724
- export { type AdvancedOidcOptions, CONFIG_PRESETS, type ConfigPreset, type DeviceAuthorizationResponse, type DeviceFlowError, DeviceLogin, type DeviceLoginProps, type EventCallbacks, type FeatureFlags, type KeycloakConfig, KeycloakConfigBuilder, type KeycloakDisabledReason, KeycloakProvider, type KeycloakProviderFullConfigProps, type KeycloakProviderProps, type KeycloakProviderZeroConfigProps, type KeycloakStatus, KeycloakStatusProvider, LoginButton, type LoginButtonProps, LogoutButton, type LogoutButtonProps, type PolicyContext, type PolicyFunction, ProtectedRoute, type ProtectedRouteProps, SessionMonitor, type SessionMonitorProps, type SilentRenewConfig, TokenBridge, type TokenBridgeProps, TokenBridgeSync, TokenLifecycleManager, type TokenLifecycleManagerProps, type TokenListener, type TokenResponse, type UseProtectedFetchOptions, type UseProtectedFetchReturn, type UseRolesReturn, type UseScopesReturn, type UseSignalRConnectionOptions, type UseSignalRConnectionReturn, type UseTokenLifecycleOptions, type UseTokenLifecycleReturn, UserProfile, type UserProfileProps, createTokenBridge, defaultConfig, defaultTokenBridge, fetchUserInfo, getConfigFromEnv, getConfigPreset, mergeWithPreset, pollForToken, requestDeviceCode, useAuth, useKeycloakStatus, useProtectedFetch, useRoles, useScopes, useSignalRConnection, useTokenLifecycle };
1749
+ export { type AdvancedOidcOptions, CONFIG_PRESETS, type ConfigPreset, type DeviceAuthorizationResponse, type DeviceFlowError, DeviceLogin, type DeviceLoginProps, type EventCallbacks, type FeatureFlags, type KeycloakConfig, KeycloakConfigBuilder, type KeycloakDisabledReason, KeycloakProvider, type KeycloakProviderFullConfigProps, type KeycloakProviderProps, type KeycloakProviderZeroConfigProps, type KeycloakStatus, KeycloakStatusProvider, LoginButton, type LoginButtonProps, LogoutButton, type LogoutButtonProps, type PolicyContext, type PolicyFunction, ProtectedRoute, type ProtectedRouteProps, SessionMonitor, type SessionMonitorProps, type SilentRenewConfig, type StoredOidcSessionArgs, type StoredOidcSessionInfo, type StoredOidcSessionUser, TokenBridge, type TokenBridgeProps, TokenBridgeSync, TokenLifecycleManager, type TokenLifecycleManagerProps, type TokenListener, type TokenResponse, type UseProtectedFetchOptions, type UseProtectedFetchReturn, type UseRolesReturn, type UseScopesReturn, type UseSignalRConnectionOptions, type UseSignalRConnectionReturn, type UseStoredOidcSessionReturn, type UseTokenLifecycleOptions, type UseTokenLifecycleReturn, UserProfile, type UserProfileProps, createTokenBridge, defaultConfig, defaultTokenBridge, fetchUserInfo, getConfigFromEnv, getConfigPreset, getOidcUserStorageKey, getStoredOidcSession, getStoredOidcUser, hasStoredOidcRefreshToken, mergeWithPreset, pollForToken, requestDeviceCode, useAuth, useKeycloakStatus, useProtectedFetch, useRoles, useScopes, useSignalRConnection, useStoredOidcSession, useTokenLifecycle };
package/dist/index.js CHANGED
@@ -1995,6 +1995,59 @@ function useProtectedFetch(options = {}) {
1995
1995
  };
1996
1996
  }
1997
1997
 
1998
+ // src/utils/storedOidcSession.ts
1999
+ var getOidcUserStorageKey = (authority, clientId) => {
2000
+ if (!authority || !clientId) {
2001
+ return null;
2002
+ }
2003
+ return `oidc.user:${authority}:${clientId}`;
2004
+ };
2005
+ var getStoredOidcUser = ({
2006
+ authority,
2007
+ clientId,
2008
+ storage
2009
+ }) => {
2010
+ const storageKey = getOidcUserStorageKey(authority, clientId);
2011
+ if (!storageKey || !storage) {
2012
+ return null;
2013
+ }
2014
+ try {
2015
+ const storedUser = storage.getItem(storageKey);
2016
+ if (!storedUser) {
2017
+ return null;
2018
+ }
2019
+ return JSON.parse(storedUser);
2020
+ } catch {
2021
+ return null;
2022
+ }
2023
+ };
2024
+ var hasStoredOidcRefreshToken = (args) => {
2025
+ const storedUser = getStoredOidcUser(args);
2026
+ return typeof storedUser?.refresh_token === "string" && storedUser.refresh_token.length > 0;
2027
+ };
2028
+ var getStoredOidcSession = (args) => {
2029
+ const storageKey = getOidcUserStorageKey(args.authority, args.clientId);
2030
+ const storedUser = getStoredOidcUser(args);
2031
+ return {
2032
+ storageKey,
2033
+ storedUser,
2034
+ hasStoredUser: storedUser !== null,
2035
+ hasRefreshToken: typeof storedUser?.refresh_token === "string" && storedUser.refresh_token.length > 0
2036
+ };
2037
+ };
2038
+
2039
+ // src/hooks/useStoredOidcSession.ts
2040
+ var useStoredOidcSession = () => {
2041
+ const auth = useAuth();
2042
+ const { storage } = useStorageConfig();
2043
+ const authority = auth.settings.authority;
2044
+ const clientId = auth.settings.client_id;
2045
+ return react.useMemo(
2046
+ () => getStoredOidcSession({ authority, clientId, storage }),
2047
+ [authority, clientId, storage]
2048
+ );
2049
+ };
2050
+
1998
2051
  // node_modules/@microsoft/signalr/dist/esm/Errors.js
1999
2052
  var HttpError = class extends Error {
2000
2053
  /** Constructs a new instance of {@link @microsoft/signalr.HttpError}.
@@ -5052,6 +5105,10 @@ exports.defaultTokenBridge = defaultTokenBridge;
5052
5105
  exports.fetchUserInfo = fetchUserInfo;
5053
5106
  exports.getConfigFromEnv = getConfigFromEnv;
5054
5107
  exports.getConfigPreset = getConfigPreset;
5108
+ exports.getOidcUserStorageKey = getOidcUserStorageKey;
5109
+ exports.getStoredOidcSession = getStoredOidcSession;
5110
+ exports.getStoredOidcUser = getStoredOidcUser;
5111
+ exports.hasStoredOidcRefreshToken = hasStoredOidcRefreshToken;
5055
5112
  exports.mergeWithPreset = mergeWithPreset;
5056
5113
  exports.pollForToken = pollForToken;
5057
5114
  exports.requestDeviceCode = requestDeviceCode;
@@ -5061,6 +5118,7 @@ exports.useProtectedFetch = useProtectedFetch;
5061
5118
  exports.useRoles = useRoles;
5062
5119
  exports.useScopes = useScopes;
5063
5120
  exports.useSignalRConnection = useSignalRConnection;
5121
+ exports.useStoredOidcSession = useStoredOidcSession;
5064
5122
  exports.useTokenLifecycle = useTokenLifecycle;
5065
5123
  //# sourceMappingURL=index.js.map
5066
5124
  //# sourceMappingURL=index.js.map