waitForCanCheckLoginState method
Implementation
@override
Future<bool> waitForCanCheckLoginState() async {
logd('waitForCanCheckLoginState called');
// If we already have auth state, return immediately
if (_hasAuthStateChangeListenerRunAtLeastOnce) {
return _fbAuth.currentUser != null;
}
// Check if we're already waiting
if (_authStateCompleter != null && !_authStateCompleter!.isCompleted) {
logd('waitForCanCheckLoginState: Already waiting, reusing existing completer');
return await _authStateCompleter!.future;
}
// Create a new completer
_authStateCompleter = Completer<bool>();
// Set up a timeout
Timer? timeoutTimer;
timeoutTimer = Timer(const Duration(seconds: 5), () async {
if (_authStateCompleter != null && !_authStateCompleter!.isCompleted) {
logw('waitForCanCheckLoginState timed out');
try {
final currentUser = _fbAuth.currentUser;
if (currentUser != null) {
// Reload the current user to force authStateChanges to trigger
await currentUser.reload();
_hasAuthStateChangeListenerRunAtLeastOnce = true;
if (!_authStateCompleter!.isCompleted) {
// Add this check
_authStateCompleter!.complete(true);
}
} else {
// Sign out to force authStateChanges to trigger
await signOut();
_hasAuthStateChangeListenerRunAtLeastOnce = true;
if (!_authStateCompleter!.isCompleted) {
// Add this check
_authStateCompleter!.complete(false);
}
}
} catch (e) {
logd('waitForCanCheckLoginState: Error during timeout handling: $e');
_hasAuthStateChangeListenerRunAtLeastOnce = true;
if (!_authStateCompleter!.isCompleted) {
// Add this check
_authStateCompleter!.complete(false);
}
}
}
});
// Wait for auth state to be ready
final result = await _authStateCompleter!.future;
timeoutTimer.cancel();
return result;
}