@oxyhq/services 5.20.2 → 5.21.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.
Files changed (42) hide show
  1. package/lib/commonjs/core/mixins/OxyServices.fedcm.js +158 -19
  2. package/lib/commonjs/core/mixins/OxyServices.fedcm.js.map +1 -1
  3. package/lib/commonjs/core/mixins/OxyServices.popup.js +40 -1
  4. package/lib/commonjs/core/mixins/OxyServices.popup.js.map +1 -1
  5. package/lib/commonjs/ui/context/OxyContext.js +19 -1
  6. package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
  7. package/lib/commonjs/ui/hooks/useAuth.js +9 -19
  8. package/lib/commonjs/ui/hooks/useAuth.js.map +1 -1
  9. package/lib/commonjs/ui/hooks/useWebSSO.js +60 -0
  10. package/lib/commonjs/ui/hooks/useWebSSO.js.map +1 -1
  11. package/lib/module/core/mixins/OxyServices.fedcm.js +158 -19
  12. package/lib/module/core/mixins/OxyServices.fedcm.js.map +1 -1
  13. package/lib/module/core/mixins/OxyServices.popup.js +40 -1
  14. package/lib/module/core/mixins/OxyServices.popup.js.map +1 -1
  15. package/lib/module/ui/context/OxyContext.js +19 -1
  16. package/lib/module/ui/context/OxyContext.js.map +1 -1
  17. package/lib/module/ui/hooks/useAuth.js +9 -19
  18. package/lib/module/ui/hooks/useAuth.js.map +1 -1
  19. package/lib/module/ui/hooks/useWebSSO.js +60 -0
  20. package/lib/module/ui/hooks/useWebSSO.js.map +1 -1
  21. package/lib/typescript/commonjs/core/mixins/OxyServices.fedcm.d.ts +1 -0
  22. package/lib/typescript/commonjs/core/mixins/OxyServices.fedcm.d.ts.map +1 -1
  23. package/lib/typescript/commonjs/core/mixins/OxyServices.popup.d.ts.map +1 -1
  24. package/lib/typescript/commonjs/ui/context/OxyContext.d.ts +11 -0
  25. package/lib/typescript/commonjs/ui/context/OxyContext.d.ts.map +1 -1
  26. package/lib/typescript/commonjs/ui/hooks/useAuth.d.ts.map +1 -1
  27. package/lib/typescript/commonjs/ui/hooks/useWebSSO.d.ts +2 -0
  28. package/lib/typescript/commonjs/ui/hooks/useWebSSO.d.ts.map +1 -1
  29. package/lib/typescript/module/core/mixins/OxyServices.fedcm.d.ts +1 -0
  30. package/lib/typescript/module/core/mixins/OxyServices.fedcm.d.ts.map +1 -1
  31. package/lib/typescript/module/core/mixins/OxyServices.popup.d.ts.map +1 -1
  32. package/lib/typescript/module/ui/context/OxyContext.d.ts +11 -0
  33. package/lib/typescript/module/ui/context/OxyContext.d.ts.map +1 -1
  34. package/lib/typescript/module/ui/hooks/useAuth.d.ts.map +1 -1
  35. package/lib/typescript/module/ui/hooks/useWebSSO.d.ts +2 -0
  36. package/lib/typescript/module/ui/hooks/useWebSSO.d.ts.map +1 -1
  37. package/package.json +1 -1
  38. package/src/core/mixins/OxyServices.fedcm.ts +160 -20
  39. package/src/core/mixins/OxyServices.popup.ts +39 -1
  40. package/src/ui/context/OxyContext.tsx +34 -0
  41. package/src/ui/hooks/useAuth.ts +9 -20
  42. package/src/ui/hooks/useWebSSO.ts +71 -0
@@ -46,7 +46,8 @@ export function OxyServicesFedCMMixin<T extends typeof OxyServicesBase>(Base: T)
46
46
  super(...(args as [any]));
47
47
  }
48
48
  public static readonly DEFAULT_CONFIG_URL = 'https://auth.oxy.so/fedcm.json';
49
- public static readonly FEDCM_TIMEOUT = 60000; // 1 minute
49
+ public static readonly FEDCM_TIMEOUT = 60000; // 1 minute for interactive
50
+ public static readonly FEDCM_SILENT_TIMEOUT = 10000; // 10 seconds for silent mediation
50
51
 
51
52
  /**
52
53
  * Check if FedCM is supported in the current browser
@@ -98,6 +99,10 @@ export function OxyServicesFedCMMixin<T extends typeof OxyServicesBase>(Base: T)
98
99
  const nonce = options.nonce || this.generateNonce();
99
100
  const clientId = this.getClientId();
100
101
 
102
+ if (typeof __DEV__ !== 'undefined' && __DEV__) {
103
+ console.log('[FedCM] Interactive sign-in: Requesting credential for', clientId);
104
+ }
105
+
101
106
  // Request credential from browser's native identity flow
102
107
  const credential = await this.requestIdentityCredential({
103
108
  configURL: (this.constructor as any).DEFAULT_CONFIG_URL,
@@ -110,6 +115,10 @@ export function OxyServicesFedCMMixin<T extends typeof OxyServicesBase>(Base: T)
110
115
  throw new OxyAuthenticationError('No credential received from browser');
111
116
  }
112
117
 
118
+ if (typeof __DEV__ !== 'undefined' && __DEV__) {
119
+ console.log('[FedCM] Interactive sign-in: Got credential, exchanging for session');
120
+ }
121
+
113
122
  // Exchange FedCM ID token for Oxy session
114
123
  const session = await this.exchangeIdTokenForSession(credential.token);
115
124
 
@@ -118,8 +127,15 @@ export function OxyServicesFedCMMixin<T extends typeof OxyServicesBase>(Base: T)
118
127
  this.httpService.setTokens((session as any).accessToken);
119
128
  }
120
129
 
130
+ if (typeof __DEV__ !== 'undefined' && __DEV__) {
131
+ console.log('[FedCM] Interactive sign-in: Success!', { userId: (session as any)?.user?.id });
132
+ }
133
+
121
134
  return session;
122
135
  } catch (error) {
136
+ if (typeof __DEV__ !== 'undefined' && __DEV__) {
137
+ console.log('[FedCM] Interactive sign-in failed:', error);
138
+ }
123
139
  if ((error as any).name === 'AbortError') {
124
140
  throw new OxyAuthenticationError('Sign-in was cancelled by user');
125
141
  }
@@ -162,35 +178,102 @@ export function OxyServicesFedCMMixin<T extends typeof OxyServicesBase>(Base: T)
162
178
  */
163
179
  async silentSignInWithFedCM(): Promise<SessionLoginResponse | null> {
164
180
  if (!this.isFedCMSupported()) {
181
+ console.log('[FedCM] Silent SSO: FedCM not supported in this browser');
165
182
  return null;
166
183
  }
167
184
 
185
+ const clientId = this.getClientId();
186
+ console.log('[FedCM] Silent SSO: Starting for', clientId);
187
+
188
+ // First try silent mediation (no UI) - works if user previously consented
189
+ let credential: { token: string } | null = null;
190
+
168
191
  try {
169
192
  const nonce = this.generateNonce();
170
- const clientId = this.getClientId();
193
+ console.log('[FedCM] Silent SSO: Attempting silent mediation...');
171
194
 
172
- // Request credential with silent mediation (no UI)
173
- const credential = await this.requestIdentityCredential({
195
+ credential = await this.requestIdentityCredential({
174
196
  configURL: (this.constructor as any).DEFAULT_CONFIG_URL,
175
197
  clientId,
176
198
  nonce,
177
199
  mediation: 'silent',
178
200
  });
179
201
 
180
- if (!credential || !credential.token) {
202
+ console.log('[FedCM] Silent SSO: Silent mediation result:', { hasCredential: !!credential, hasToken: !!credential?.token });
203
+ } catch (silentError) {
204
+ // Silent mediation failed - this is expected if user hasn't consented before or is in quiet period
205
+ const errorName = silentError instanceof Error ? silentError.name : 'Unknown';
206
+ const errorMessage = silentError instanceof Error ? silentError.message : String(silentError);
207
+ console.log('[FedCM] Silent SSO: Silent mediation error (will try optional):', { name: errorName, message: errorMessage });
208
+ }
209
+
210
+ // If silent failed, try optional mediation which shows browser UI if needed
211
+ if (!credential || !credential.token) {
212
+ try {
213
+ const nonce = this.generateNonce();
214
+ console.log('[FedCM] Silent SSO: Trying optional mediation (may show browser UI)...');
215
+
216
+ credential = await this.requestIdentityCredential({
217
+ configURL: (this.constructor as any).DEFAULT_CONFIG_URL,
218
+ clientId,
219
+ nonce,
220
+ mediation: 'optional',
221
+ });
222
+
223
+ console.log('[FedCM] Silent SSO: Optional mediation result:', { hasCredential: !!credential, hasToken: !!credential?.token });
224
+ } catch (optionalError) {
225
+ const errorName = optionalError instanceof Error ? optionalError.name : 'Unknown';
226
+ const errorMessage = optionalError instanceof Error ? optionalError.message : String(optionalError);
227
+ console.log('[FedCM] Silent SSO: Optional mediation also failed:', { name: errorName, message: errorMessage });
181
228
  return null;
182
229
  }
230
+ }
183
231
 
184
- const session = await this.exchangeIdTokenForSession(credential.token);
185
- if (session && (session as any).accessToken) {
186
- this.httpService.setTokens((session as any).accessToken);
187
- }
232
+ if (!credential || !credential.token) {
233
+ console.log('[FedCM] Silent SSO: No credential returned (user may have dismissed prompt or is not logged in at IdP)');
234
+ return null;
235
+ }
188
236
 
189
- return session;
190
- } catch (error) {
191
- // Silent failures are expected and should not throw
237
+ console.log('[FedCM] Silent SSO: Got credential, exchanging for session...');
238
+
239
+ let session: SessionLoginResponse;
240
+ try {
241
+ session = await this.exchangeIdTokenForSession(credential.token);
242
+ } catch (exchangeError) {
243
+ console.error('[FedCM] Silent SSO: Token exchange failed:', exchangeError);
244
+ return null;
245
+ }
246
+
247
+ // Validate session response has required fields
248
+ if (!session) {
249
+ console.error('[FedCM] Silent SSO: Exchange returned null session');
250
+ return null;
251
+ }
252
+
253
+ if (!session.sessionId) {
254
+ console.error('[FedCM] Silent SSO: Exchange returned session without sessionId:', session);
255
+ return null;
256
+ }
257
+
258
+ if (!session.user) {
259
+ console.error('[FedCM] Silent SSO: Exchange returned session without user:', session);
192
260
  return null;
193
261
  }
262
+
263
+ // Set the access token
264
+ if ((session as any).accessToken) {
265
+ this.httpService.setTokens((session as any).accessToken);
266
+ console.log('[FedCM] Silent SSO: Access token set');
267
+ } else {
268
+ console.warn('[FedCM] Silent SSO: No accessToken in session response');
269
+ }
270
+
271
+ console.log('[FedCM] Silent SSO: Success!', {
272
+ sessionId: session.sessionId?.substring(0, 8) + '...',
273
+ userId: session.user?.id
274
+ });
275
+
276
+ return session;
194
277
  }
195
278
 
196
279
  /**
@@ -213,8 +296,15 @@ export function OxyServicesFedCMMixin<T extends typeof OxyServicesBase>(Base: T)
213
296
  const requestedMediation = options.mediation || 'optional';
214
297
  const isInteractive = requestedMediation !== 'silent';
215
298
 
299
+ console.log('[FedCM] requestIdentityCredential called:', {
300
+ mediation: requestedMediation,
301
+ clientId: options.clientId,
302
+ inProgress: fedCMRequestInProgress,
303
+ });
304
+
216
305
  // If a request is already in progress...
217
306
  if (fedCMRequestInProgress && fedCMRequestPromise) {
307
+ console.log('[FedCM] Request already in progress, waiting...');
218
308
  // If current request is silent and new request is interactive,
219
309
  // wait for silent to finish, then make the interactive request
220
310
  if (currentMediationMode === 'silent' && isInteractive) {
@@ -237,10 +327,18 @@ export function OxyServicesFedCMMixin<T extends typeof OxyServicesBase>(Base: T)
237
327
  fedCMRequestInProgress = true;
238
328
  currentMediationMode = requestedMediation;
239
329
  const controller = new AbortController();
240
- const timeout = setTimeout(() => controller.abort(), (this.constructor as any).FEDCM_TIMEOUT);
330
+ // Use shorter timeout for silent mediation since it should be quick
331
+ const timeoutMs = requestedMediation === 'silent'
332
+ ? (this.constructor as any).FEDCM_SILENT_TIMEOUT
333
+ : (this.constructor as any).FEDCM_TIMEOUT;
334
+ const timeout = setTimeout(() => {
335
+ console.log('[FedCM] Request timed out after', timeoutMs, 'ms (mediation:', requestedMediation + ')');
336
+ controller.abort();
337
+ }, timeoutMs);
241
338
 
242
339
  fedCMRequestPromise = (async () => {
243
340
  try {
341
+ console.log('[FedCM] Calling navigator.credentials.get with mediation:', requestedMediation);
244
342
  // Type assertion needed as FedCM types may not be in all TypeScript versions
245
343
  const credential = (await (navigator.credentials as any).get({
246
344
  identity: {
@@ -248,7 +346,11 @@ export function OxyServicesFedCMMixin<T extends typeof OxyServicesBase>(Base: T)
248
346
  {
249
347
  configURL: options.configURL,
250
348
  clientId: options.clientId,
251
- nonce: options.nonce,
349
+ // Send nonce at both levels for backward compatibility
350
+ nonce: options.nonce, // For older browsers
351
+ params: {
352
+ nonce: options.nonce, // For Chrome 145+
353
+ },
252
354
  ...(options.context && { loginHint: options.context }),
253
355
  },
254
356
  ],
@@ -257,11 +359,24 @@ export function OxyServicesFedCMMixin<T extends typeof OxyServicesBase>(Base: T)
257
359
  signal: controller.signal,
258
360
  })) as any;
259
361
 
362
+ console.log('[FedCM] navigator.credentials.get returned:', {
363
+ hasCredential: !!credential,
364
+ type: credential?.type,
365
+ hasToken: !!credential?.token,
366
+ });
367
+
260
368
  if (!credential || credential.type !== 'identity') {
369
+ console.log('[FedCM] No valid identity credential returned');
261
370
  return null;
262
371
  }
263
372
 
373
+ console.log('[FedCM] Got valid identity credential with token');
264
374
  return { token: credential.token };
375
+ } catch (error) {
376
+ const errorName = error instanceof Error ? error.name : 'Unknown';
377
+ const errorMessage = error instanceof Error ? error.message : String(error);
378
+ console.log('[FedCM] navigator.credentials.get error:', { name: errorName, message: errorMessage });
379
+ throw error;
265
380
  } finally {
266
381
  clearTimeout(timeout);
267
382
  fedCMRequestInProgress = false;
@@ -282,12 +397,37 @@ export function OxyServicesFedCMMixin<T extends typeof OxyServicesBase>(Base: T)
282
397
  * @private
283
398
  */
284
399
  public async exchangeIdTokenForSession(idToken: string): Promise<SessionLoginResponse> {
285
- return this.makeRequest<SessionLoginResponse>(
286
- 'POST',
287
- '/api/fedcm/exchange',
288
- { id_token: idToken },
289
- { cache: false }
290
- );
400
+ console.log('[FedCM] exchangeIdTokenForSession: Starting exchange...');
401
+ console.log('[FedCM] exchangeIdTokenForSession: Token length:', idToken?.length);
402
+ console.log('[FedCM] exchangeIdTokenForSession: Token preview:', idToken?.substring(0, 50) + '...');
403
+
404
+ try {
405
+ const response = await this.makeRequest<SessionLoginResponse>(
406
+ 'POST',
407
+ '/api/fedcm/exchange',
408
+ { id_token: idToken },
409
+ { cache: false }
410
+ );
411
+
412
+ console.log('[FedCM] exchangeIdTokenForSession: Response received:', {
413
+ hasResponse: !!response,
414
+ hasSessionId: !!(response as any)?.sessionId,
415
+ hasUser: !!(response as any)?.user,
416
+ hasAccessToken: !!(response as any)?.accessToken,
417
+ userId: (response as any)?.user?.id,
418
+ username: (response as any)?.user?.username,
419
+ responseKeys: response ? Object.keys(response) : [],
420
+ });
421
+
422
+ return response;
423
+ } catch (error) {
424
+ console.error('[FedCM] exchangeIdTokenForSession: Error:', {
425
+ name: error instanceof Error ? error.name : 'Unknown',
426
+ message: error instanceof Error ? error.message : String(error),
427
+ stack: error instanceof Error ? error.stack : undefined,
428
+ });
429
+ throw error;
430
+ }
291
431
  }
292
432
 
293
433
  /**
@@ -111,6 +111,25 @@ export function OxyServicesPopupAuthMixin<T extends typeof OxyServicesBase>(Base
111
111
  this.httpService.setTokens((session as any).accessToken);
112
112
  }
113
113
 
114
+ // Fetch user data using the session ID
115
+ // The callback page only sends sessionId/accessToken, not user data
116
+ if (session && session.sessionId && !session.user) {
117
+ try {
118
+ const userData = await this.makeRequest<any>(
119
+ 'GET',
120
+ `/api/session/user/${session.sessionId}`,
121
+ undefined,
122
+ { cache: false }
123
+ );
124
+ if (userData) {
125
+ (session as any).user = userData;
126
+ }
127
+ } catch (userError) {
128
+ console.warn('[PopupAuth] Failed to fetch user data:', userError);
129
+ // Continue without user data - caller can fetch separately
130
+ }
131
+ }
132
+
114
133
  return session;
115
134
  } catch (error) {
116
135
  throw error;
@@ -238,8 +257,21 @@ export function OxyServicesPopupAuthMixin<T extends typeof OxyServicesBase>(Base
238
257
  }, timeout);
239
258
 
240
259
  const messageHandler = (event: MessageEvent) => {
260
+ const authUrl = (this.constructor as any).AUTH_URL;
261
+
262
+ // Log all messages for debugging
263
+ if (event.data && typeof event.data === 'object' && event.data.type) {
264
+ console.log('[PopupAuth] Message received:', {
265
+ origin: event.origin,
266
+ expectedOrigin: authUrl,
267
+ type: event.data.type,
268
+ hasSession: !!event.data.session,
269
+ hasError: !!event.data.error,
270
+ });
271
+ }
272
+
241
273
  // CRITICAL: Verify origin to prevent XSS attacks
242
- if (event.origin !== (this.constructor as any).AUTH_URL) {
274
+ if (event.origin !== authUrl) {
243
275
  return;
244
276
  }
245
277
 
@@ -249,9 +281,12 @@ export function OxyServicesPopupAuthMixin<T extends typeof OxyServicesBase>(Base
249
281
  return;
250
282
  }
251
283
 
284
+ console.log('[PopupAuth] Valid auth response:', { state, expectedState, hasSession: !!session, error });
285
+
252
286
  // Verify state parameter to prevent CSRF attacks
253
287
  if (state !== expectedState) {
254
288
  cleanup();
289
+ console.error('[PopupAuth] State mismatch');
255
290
  reject(new OxyAuthenticationError('Invalid state parameter. Possible CSRF attack.'));
256
291
  return;
257
292
  }
@@ -259,10 +294,13 @@ export function OxyServicesPopupAuthMixin<T extends typeof OxyServicesBase>(Base
259
294
  cleanup();
260
295
 
261
296
  if (error) {
297
+ console.error('[PopupAuth] Auth error:', error);
262
298
  reject(new OxyAuthenticationError(error));
263
299
  } else if (session) {
300
+ console.log('[PopupAuth] Session received successfully');
264
301
  resolve(session);
265
302
  } else {
303
+ console.error('[PopupAuth] No session in response');
266
304
  reject(new OxyAuthenticationError('No session received from authentication server'));
267
305
  }
268
306
  };
@@ -55,6 +55,18 @@ export interface OxyContextState {
55
55
  // Authentication
56
56
  signIn: (publicKey: string, deviceName?: string) => Promise<User>;
57
57
 
58
+ /**
59
+ * Handle session from popup authentication
60
+ * Updates auth state, persists session to storage
61
+ */
62
+ handlePopupSession: (session: {
63
+ sessionId: string;
64
+ accessToken?: string;
65
+ expiresAt?: string;
66
+ user: User;
67
+ deviceId?: string;
68
+ }) => Promise<void>;
69
+
58
70
  // Session management
59
71
  logout: (targetSessionId?: string) => Promise<void>;
60
72
  logoutAll: () => Promise<void>;
@@ -414,11 +426,23 @@ export const OxyProvider: React.FC<OxyContextProviderProps> = ({
414
426
  }, [restoreSessionsFromStorage, storage]);
415
427
 
416
428
  // Web SSO: Automatically check for cross-domain session on web platforms
429
+ // Also used for popup auth - updates all state and persists session
417
430
  const handleWebSSOSession = useCallback(async (session: any) => {
431
+ console.log('[OxyContext] handleWebSSOSession called:', {
432
+ hasSession: !!session,
433
+ hasUser: !!session?.user,
434
+ hasSessionId: !!session?.sessionId,
435
+ sessionIdPrefix: session?.sessionId?.substring(0, 8),
436
+ userId: session?.user?.id,
437
+ });
438
+
418
439
  if (!session?.user || !session?.sessionId) {
440
+ console.warn('[OxyContext] handleWebSSOSession: Invalid session - missing user or sessionId');
419
441
  return;
420
442
  }
421
443
 
444
+ console.log('[OxyContext] handleWebSSOSession: Processing valid session...');
445
+
422
446
  // Update sessions state
423
447
  const clientSession = {
424
448
  sessionId: session.sessionId,
@@ -443,7 +467,15 @@ export const OxyProvider: React.FC<OxyContextProviderProps> = ({
443
467
  sessionIds.push(session.sessionId);
444
468
  await storage.setItem(storageKeys.sessionIds, JSON.stringify(sessionIds));
445
469
  }
470
+ console.log('[OxyContext] handleWebSSOSession: Session persisted to storage', {
471
+ sessionId: session.sessionId?.substring(0, 8),
472
+ totalSessions: sessionIds.length,
473
+ });
474
+ } else {
475
+ console.warn('[OxyContext] handleWebSSOSession: No storage available, session not persisted!');
446
476
  }
477
+
478
+ console.log('[OxyContext] handleWebSSOSession: Complete! User should now be authenticated.');
447
479
  }, [updateSessions, setActiveSessionId, loginSuccess, onAuthStateChange, storage, storageKeys]);
448
480
 
449
481
  // Enable web SSO only after local storage check completes and no user found
@@ -571,6 +603,7 @@ export const OxyProvider: React.FC<OxyContextProviderProps> = ({
571
603
  hasIdentity,
572
604
  getPublicKey,
573
605
  signIn,
606
+ handlePopupSession: handleWebSSOSession,
574
607
  logout,
575
608
  logoutAll,
576
609
  switchSession: switchSessionForContext,
@@ -589,6 +622,7 @@ export const OxyProvider: React.FC<OxyContextProviderProps> = ({
589
622
  }), [
590
623
  activeSessionId,
591
624
  signIn,
625
+ handleWebSSOSession,
592
626
  currentLanguage,
593
627
  currentLanguageMetadata,
594
628
  currentLanguageName,
@@ -91,6 +91,7 @@ export function useAuth(): UseAuthReturn {
91
91
  isTokenReady,
92
92
  error,
93
93
  signIn: oxySignIn,
94
+ handlePopupSession,
94
95
  logout,
95
96
  logoutAll,
96
97
  refreshSessions,
@@ -106,37 +107,25 @@ export function useAuth(): UseAuthReturn {
106
107
  const isIdentityProvider = isWebBrowser() &&
107
108
  window.location.hostname === 'auth.oxy.so';
108
109
 
109
- // Web (not on IdP): Use FedCM or popup-based authentication
110
+ // Web (not on IdP): Use popup-based authentication
111
+ // We go straight to popup to preserve the "user gesture" (click event)
112
+ // FedCM silent SSO already runs on page load via useWebSSO
113
+ // If user is clicking "Sign In", they need interactive auth NOW
110
114
  if (isWebBrowser() && !publicKey && !isIdentityProvider) {
111
- // Try FedCM first (instant if user already signed in at IdP)
112
- if ((oxyServices as any).isFedCMSupported?.()) {
113
- try {
114
- const fedcmSession = await (oxyServices as any).signInWithFedCM?.();
115
- if (fedcmSession?.user) {
116
- return fedcmSession.user;
117
- }
118
- } catch (fedcmError) {
119
- // FedCM failed (user not signed in at IdP, cancelled, etc.)
120
- // Fall through to popup
121
- console.debug('FedCM failed, falling back to popup:', fedcmError);
122
- }
123
- }
124
-
125
- // Fallback to popup (opens auth.oxy.so in popup window)
126
115
  try {
127
116
  const popupSession = await (oxyServices as any).signInWithPopup?.();
128
117
  if (popupSession?.user) {
118
+ // Update context state with the session (this updates user, sessions, storage)
119
+ await handlePopupSession(popupSession);
129
120
  return popupSession.user;
130
121
  }
122
+ throw new Error('Sign-in failed. Please try again.');
131
123
  } catch (popupError) {
132
- // If popup blocked, suggest enabling popups
133
124
  if (popupError instanceof Error && popupError.message.includes('blocked')) {
134
125
  throw new Error('Popup blocked. Please allow popups for this site.');
135
126
  }
136
127
  throw popupError;
137
128
  }
138
-
139
- throw new Error('Sign-in failed. Please try again.');
140
129
  }
141
130
 
142
131
  // Native: Use cryptographic identity
@@ -174,7 +163,7 @@ export function useAuth(): UseAuthReturn {
174
163
  }
175
164
 
176
165
  throw new Error('No authentication method available');
177
- }, [oxySignIn, hasIdentity, getPublicKey, showBottomSheet, oxyServices]);
166
+ }, [oxySignIn, hasIdentity, getPublicKey, showBottomSheet, oxyServices, handlePopupSession]);
178
167
 
179
168
  const signOut = useCallback(async (): Promise<void> => {
180
169
  await logout();
@@ -30,6 +30,8 @@ interface UseWebSSOOptions {
30
30
  interface UseWebSSOResult {
31
31
  /** Manually trigger SSO check */
32
32
  checkSSO: () => Promise<SessionLoginResponse | null>;
33
+ /** Trigger interactive FedCM sign-in (shows browser UI) */
34
+ signInWithFedCM: () => Promise<SessionLoginResponse | null>;
33
35
  /** Whether SSO check is in progress */
34
36
  isChecking: boolean;
35
37
  /** Whether FedCM is supported in this browser */
@@ -85,12 +87,20 @@ export function useWebSSO({
85
87
  const fedCMSupported = isWebBrowser() && (oxyServices as any).isFedCMSupported?.();
86
88
 
87
89
  const checkSSO = useCallback(async (): Promise<SessionLoginResponse | null> => {
90
+ console.log('[useWebSSO] checkSSO called', {
91
+ isWebBrowser: isWebBrowser(),
92
+ isChecking: isCheckingRef.current,
93
+ isIdP: isIdentityProvider(),
94
+ fedCMSupported,
95
+ });
96
+
88
97
  if (!isWebBrowser() || isCheckingRef.current) {
89
98
  return null;
90
99
  }
91
100
 
92
101
  // Don't use FedCM on the auth domain itself - it would authenticate against itself
93
102
  if (isIdentityProvider()) {
103
+ console.log('[useWebSSO] Skipping - on identity provider domain');
94
104
  onSSOUnavailable?.();
95
105
  return null;
96
106
  }
@@ -98,27 +108,39 @@ export function useWebSSO({
98
108
  // FedCM is the only reliable cross-domain SSO mechanism
99
109
  // Third-party cookies are deprecated and unreliable
100
110
  if (!fedCMSupported) {
111
+ console.log('[useWebSSO] Skipping - FedCM not supported');
101
112
  onSSOUnavailable?.();
102
113
  return null;
103
114
  }
104
115
 
105
116
  isCheckingRef.current = true;
117
+ console.log('[useWebSSO] Starting FedCM silent sign-in...');
106
118
 
107
119
  try {
108
120
  // Use FedCM for cross-domain SSO
109
121
  // This works because browser treats IdP requests as first-party
110
122
  const session = await (oxyServices as any).silentSignInWithFedCM?.();
111
123
 
124
+ console.log('[useWebSSO] FedCM result:', {
125
+ hasSession: !!session,
126
+ hasUser: !!session?.user,
127
+ hasSessionId: !!session?.sessionId,
128
+ });
129
+
112
130
  if (session) {
131
+ console.log('[useWebSSO] Session found, calling onSessionFound...');
113
132
  await onSessionFound(session);
133
+ console.log('[useWebSSO] onSessionFound completed');
114
134
  return session;
115
135
  }
116
136
 
117
137
  // No session found - user needs to sign in
138
+ console.log('[useWebSSO] No session returned from FedCM');
118
139
  onSSOUnavailable?.();
119
140
  return null;
120
141
  } catch (error) {
121
142
  // FedCM failed - could be network error, user not signed in, etc.
143
+ console.error('[useWebSSO] FedCM error:', error);
122
144
  onSSOUnavailable?.();
123
145
  onError?.(error instanceof Error ? error : new Error(String(error)));
124
146
  return null;
@@ -127,6 +149,54 @@ export function useWebSSO({
127
149
  }
128
150
  }, [oxyServices, onSessionFound, onSSOUnavailable, onError, fedCMSupported]);
129
151
 
152
+ /**
153
+ * Trigger interactive FedCM sign-in
154
+ * This shows the browser's native "Sign in with Oxy" prompt.
155
+ * Use this when silent mediation fails (user hasn't previously consented).
156
+ */
157
+ const signInWithFedCM = useCallback(async (): Promise<SessionLoginResponse | null> => {
158
+ console.log('[useWebSSO] signInWithFedCM called');
159
+
160
+ if (!isWebBrowser() || isCheckingRef.current) {
161
+ return null;
162
+ }
163
+
164
+ if (!fedCMSupported) {
165
+ console.log('[useWebSSO] FedCM not supported for interactive sign-in');
166
+ onError?.(new Error('FedCM is not supported in this browser'));
167
+ return null;
168
+ }
169
+
170
+ isCheckingRef.current = true;
171
+ console.log('[useWebSSO] Starting interactive FedCM sign-in...');
172
+
173
+ try {
174
+ // Use interactive sign-in (shows browser UI)
175
+ const session = await (oxyServices as any).signInWithFedCM?.();
176
+
177
+ console.log('[useWebSSO] Interactive FedCM result:', {
178
+ hasSession: !!session,
179
+ hasUser: !!session?.user,
180
+ hasSessionId: !!session?.sessionId,
181
+ });
182
+
183
+ if (session) {
184
+ console.log('[useWebSSO] Interactive session found, calling onSessionFound...');
185
+ await onSessionFound(session);
186
+ console.log('[useWebSSO] onSessionFound completed');
187
+ return session;
188
+ }
189
+
190
+ return null;
191
+ } catch (error) {
192
+ console.error('[useWebSSO] Interactive FedCM error:', error);
193
+ onError?.(error instanceof Error ? error : new Error(String(error)));
194
+ return null;
195
+ } finally {
196
+ isCheckingRef.current = false;
197
+ }
198
+ }, [oxyServices, onSessionFound, onError, fedCMSupported]);
199
+
130
200
  // Auto-check SSO on mount (web only, FedCM only, not on auth domain)
131
201
  useEffect(() => {
132
202
  if (!enabled || !isWebBrowser() || hasCheckedRef.current || isIdentityProvider()) {
@@ -148,6 +218,7 @@ export function useWebSSO({
148
218
 
149
219
  return {
150
220
  checkSSO,
221
+ signInWithFedCM,
151
222
  isChecking: isCheckingRef.current,
152
223
  isFedCMSupported: fedCMSupported,
153
224
  };