@oxyhq/auth 2.0.6 → 2.0.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oxyhq/auth",
3
- "version": "2.0.6",
3
+ "version": "2.0.7",
4
4
  "description": "OxyHQ Web Authentication SDK — headless auth with React hooks for Next.js, Vite, and web apps",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -58,6 +58,23 @@ export interface WebOxyContextValue extends WebAuthState, WebAuthActions {
58
58
 
59
59
  const WebOxyContext = createContext<WebOxyContextValue | null>(null);
60
60
 
61
+ /**
62
+ * Module-level run-once guard for FedCM silent sign-in.
63
+ *
64
+ * The init effect runs again whenever the provider remounts (route change,
65
+ * StrictMode double-invoke, error-boundary recovery). The redirect-callback
66
+ * and local-session-restore steps are cheap and idempotent, but the FedCM
67
+ * `silentSignIn()` step triggers `navigator.credentials.get`, which must fire
68
+ * AT MOST ONCE per page load — otherwise a remount storm becomes a credential
69
+ * request storm. Keyed by origin so the guard survives instance churn; never
70
+ * cleared because only a fresh page load can change the IdP session state.
71
+ */
72
+ const fedcmSilentSignInAttempted = new Set<string>();
73
+
74
+ function silentSignInKey(): string {
75
+ return typeof window !== 'undefined' ? window.location.origin : 'no-origin';
76
+ }
77
+
61
78
  export interface WebOxyProviderProps {
62
79
  children: ReactNode;
63
80
  baseURL: string;
@@ -191,14 +208,21 @@ export function WebOxyProvider({
191
208
  }
192
209
  }
193
210
 
194
- try {
195
- const session = await crossDomainAuth.silentSignIn();
196
- if (mounted && session?.user) {
197
- await handleAuthSuccess(session, 'fedcm');
198
- return;
211
+ // FedCM silent sign-in: run AT MOST ONCE per page load. A remount
212
+ // (route change / StrictMode / error recovery) must not re-trigger
213
+ // the browser credential request.
214
+ const ssoKey = silentSignInKey();
215
+ if (!fedcmSilentSignInAttempted.has(ssoKey)) {
216
+ fedcmSilentSignInAttempted.add(ssoKey);
217
+ try {
218
+ const session = await crossDomainAuth.silentSignIn();
219
+ if (mounted && session?.user) {
220
+ await handleAuthSuccess(session, 'fedcm');
221
+ return;
222
+ }
223
+ } catch {
224
+ // Silent sign-in failed — resolve to unauthenticated below.
199
225
  }
200
- } catch {
201
- // Silent sign-in failed
202
226
  }
203
227
 
204
228
  if (mounted) setIsLoading(false);
@@ -38,6 +38,36 @@ interface UseWebSSOResult {
38
38
  isFedCMSupported: boolean;
39
39
  }
40
40
 
41
+ /**
42
+ * Module-level guard tracking which (origin + API) signatures have already
43
+ * had a silent SSO attempt this page load.
44
+ *
45
+ * A per-component `useRef` guard resets whenever the provider remounts (route
46
+ * churn, StrictMode double-invoke, error-boundary recovery), which previously
47
+ * allowed silent SSO to re-fire and — combined with a routing redirect loop —
48
+ * produced an accelerating `navigator.credentials.get` retry storm. Keying the
49
+ * guard on a stable signature instead of the component instance makes silent
50
+ * SSO fire EXACTLY ONCE per page load regardless of how many times the
51
+ * provider mounts. The set is intentionally never cleared: a fresh page load
52
+ * (the only thing that can change the answer) starts a fresh module scope.
53
+ */
54
+ const silentSSOAttempted = new Set<string>();
55
+
56
+ /**
57
+ * Build a stable signature for the silent-SSO run-once guard. Two providers
58
+ * pointed at the same API from the same origin share one attempt.
59
+ */
60
+ function ssoSignature(oxyServices: OxyServices): string {
61
+ const origin = typeof window !== 'undefined' ? window.location.origin : 'no-origin';
62
+ let baseURL = '';
63
+ try {
64
+ baseURL = oxyServices.getBaseURL();
65
+ } catch {
66
+ baseURL = '';
67
+ }
68
+ return `${origin}|${baseURL}`;
69
+ }
70
+
41
71
  /**
42
72
  * Check if we're running in a web browser environment (not React Native)
43
73
  */
@@ -158,7 +188,14 @@ export function useWebSSO({
158
188
  }
159
189
  }, [oxyServices, onSessionFound, onError, fedCMSupported]);
160
190
 
161
- // Auto-check SSO on mount (web only, FedCM only, not on auth domain)
191
+ // Auto-check SSO on mount (web only, FedCM only, not on auth domain).
192
+ //
193
+ // Run-once is enforced by TWO guards:
194
+ // 1. `hasCheckedRef` — cheap per-instance fast-path so effect re-runs
195
+ // (from changing deps) within one mount never re-fire.
196
+ // 2. `silentSSOAttempted` — module-level, survives remounts/StrictMode so
197
+ // silent SSO fires exactly once per page load even if the provider
198
+ // unmounts and remounts.
162
199
  useEffect(() => {
163
200
  if (!enabled || !isWebBrowser() || hasCheckedRef.current || isIdentityProvider()) {
164
201
  if (isIdentityProvider()) {
@@ -167,14 +204,23 @@ export function useWebSSO({
167
204
  return;
168
205
  }
169
206
 
207
+ const signature = ssoSignature(oxyServices);
208
+ if (silentSSOAttempted.has(signature)) {
209
+ // Already attempted this page load (e.g. before a remount) — do not
210
+ // re-fire. Mark the local fast-path too so subsequent re-renders skip.
211
+ hasCheckedRef.current = true;
212
+ return;
213
+ }
214
+
170
215
  hasCheckedRef.current = true;
216
+ silentSSOAttempted.add(signature);
171
217
 
172
218
  if (fedCMSupported) {
173
219
  checkSSO();
174
220
  } else {
175
221
  onSSOUnavailable?.();
176
222
  }
177
- }, [enabled, checkSSO, fedCMSupported, onSSOUnavailable]);
223
+ }, [enabled, checkSSO, fedCMSupported, onSSOUnavailable, oxyServices]);
178
224
 
179
225
  return {
180
226
  checkSSO,