71 lines
2.0 KiB
Dart
71 lines
2.0 KiB
Dart
import 'dart:developer' as developer;
|
|
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:flutter/foundation.dart' show kDebugMode, kIsWeb;
|
|
|
|
/// Verbose auth logging (browser console / `journalctl` when enabled).
|
|
///
|
|
/// On by default for web builds. Force in any build:
|
|
/// --dart-define=AUTH_VERBOSE_LOG=true
|
|
const bool _authVerboseFromEnv = bool.fromEnvironment('AUTH_VERBOSE_LOG');
|
|
|
|
bool get authVerboseLoggingEnabled =>
|
|
kDebugMode || _authVerboseFromEnv || kIsWeb;
|
|
|
|
/// Structured auth diagnostics (no tokens or secrets).
|
|
void authLog(
|
|
String message, {
|
|
Object? error,
|
|
StackTrace? stackTrace,
|
|
Map<String, Object?>? context,
|
|
}) {
|
|
if (!authVerboseLoggingEnabled) {
|
|
return;
|
|
}
|
|
|
|
final StringBuffer buf = StringBuffer('[Auth] $message');
|
|
if (context != null && context.isNotEmpty) {
|
|
buf.write(' | ');
|
|
buf.write(
|
|
context.entries
|
|
.map((MapEntry<String, Object?> e) => '${e.key}=${e.value}')
|
|
.join(' '),
|
|
);
|
|
}
|
|
if (error != null) {
|
|
buf.write(' | error=${describeAuthError(error)}');
|
|
}
|
|
|
|
developer.log(
|
|
buf.toString(),
|
|
name: 'cyberhybridhub.auth',
|
|
error: error,
|
|
stackTrace: stackTrace,
|
|
);
|
|
}
|
|
|
|
/// User-facing detail string; safe to show in support UI (no secrets).
|
|
String describeAuthError(Object error) {
|
|
if (error is FirebaseAuthException) {
|
|
final StringBuffer buf = StringBuffer('FirebaseAuthException');
|
|
buf.write(' code=${error.code}');
|
|
if (error.message != null && error.message!.isNotEmpty) {
|
|
buf.write(' message=${error.message}');
|
|
}
|
|
if (error.email != null && error.email!.isNotEmpty) {
|
|
buf.write(' email=${error.email}');
|
|
}
|
|
return buf.toString();
|
|
}
|
|
return error.toString();
|
|
}
|
|
|
|
Map<String, Object?> firebaseExceptionContext(FirebaseAuthException e) {
|
|
return <String, Object?>{
|
|
'code': e.code,
|
|
if (e.message != null) 'message': e.message!,
|
|
if (e.email != null) 'email': e.email!,
|
|
if (e.credential != null) 'credentialProvider': e.credential!.providerId,
|
|
};
|
|
}
|