cyberhybridhub/lib/utils/firebase_auth_domain_check.dart
2026-06-04 19:31:04 +00:00

67 lines
1.8 KiB
Dart

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<void> 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: <String, Object?>{
'status': response.statusCode,
'host': host,
},
);
return;
}
final Object? decoded = jsonDecode(response.body);
if (decoded is! Map<String, dynamic>) {
return;
}
final List<String> domains =
(decoded['authorizedDomains'] as List<dynamic>?)
?.map((dynamic d) => d.toString())
.toList() ??
<String>[];
final bool allowed = domains.contains(host);
authLog(
allowed
? 'Firebase authorizedDomains includes this host'
: 'Firebase authorizedDomains MISSING this host — add it in Console',
context: <String, Object?>{
'host': host,
'projectId': decoded['projectId'] ?? DefaultFirebaseOptions.web.projectId,
'authorizedDomains': domains.join(', '),
},
);
} catch (e, st) {
authLog(
'Firebase authorizedDomains check failed',
error: e,
stackTrace: st,
);
}
}