diff --git a/userfront/lib/core/services/web_auth_integration_web.dart b/userfront/lib/core/services/web_auth_integration_web.dart index 493d17e7..9b3b06db 100644 --- a/userfront/lib/core/services/web_auth_integration_web.dart +++ b/userfront/lib/core/services/web_auth_integration_web.dart @@ -3,16 +3,32 @@ import 'dart:async'; import 'dart:html' as html; import 'package:flutter/foundation.dart'; +import 'auth_token_store.dart'; void implSendLoginSuccess(String token) { - final message = {'type': 'LOGIN_SUCCESS', 'token': token}; - - if (html.window.opener != null) { - try { - html.window.opener!.postMessage(message, '*'); - debugPrint('Sent login success message to opener'); - } catch (e) { - debugPrint('Failed to postMessage: $e'); + var effectiveToken = token; + if (effectiveToken.isEmpty) { + effectiveToken = AuthTokenStore.getToken() ?? ""; + } + + final fullUrl = html.window.location.href; + final uri = Uri.base; + + // Try to find redirect_uri from standard parsing first, then manual string search + String? redirectUri = + uri.queryParameters['redirect_uri'] ?? + uri.queryParameters['redirect_url']; + + if (redirectUri == null) { + // Manual fallback for cases where Uri.base misses params + final searchParams = html.window.location.search; + if (searchParams != null && searchParams.isNotEmpty) { + final sUri = Uri.parse( + '?${searchParams.startsWith('?') ? searchParams.substring(1) : searchParams}', + ); + redirectUri = + sUri.queryParameters['redirect_uri'] ?? + sUri.queryParameters['redirect_url']; } } @@ -63,9 +79,7 @@ void implSendLoginSuccess(String token) { debugPrint('Failed to close window: $e'); } }); - } else { - // Should not happen given isPopup check, but as fallback: - debugPrint('No opener found during popup flow.'); + return; } // No opener and no redirect: fall back to local navigation