import 'dart:convert'; import 'package:flutter/foundation.dart' show kIsWeb; import 'package:http/http.dart' as http; import '../firebase_options.dart'; import 'auth_log.dart'; /// Fetches live Firebase Auth authorized domains (public config API) and logs /// whether [Uri.base.host] is allowed. Helps when the console UI disagrees with /// what the backend actually has registered. Future logFirebaseAuthorizedDomainsIfWeb() async { if (!kIsWeb) { return; } final String host = Uri.base.host; final String apiKey = DefaultFirebaseOptions.web.apiKey; final Uri uri = Uri.parse( 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/getProjectConfig' '?key=$apiKey', ); try { final http.Response response = await http.get(uri); if (response.statusCode != 200) { authLog( 'could not load Firebase project config', context: { 'status': response.statusCode, 'host': host, }, ); return; } final Object? decoded = jsonDecode(response.body); if (decoded is! Map) { return; } final List domains = (decoded['authorizedDomains'] as List?) ?.map((dynamic d) => d.toString()) .toList() ?? []; final bool allowed = domains.contains(host); authLog( allowed ? 'Firebase authorizedDomains includes this host' : 'Firebase authorizedDomains MISSING this host — add it in Console', context: { 'host': host, 'projectId': decoded['projectId'] ?? DefaultFirebaseOptions.web.projectId, 'authorizedDomains': domains.join(', '), }, ); } catch (e, st) { authLog( 'Firebase authorizedDomains check failed', error: e, stackTrace: st, ); } }