handleAuthStateChanges method

Future<void> handleAuthStateChanges(
  1. User? fbUser
)

Implementation

Future<void> handleAuthStateChanges(fb_auth.User? fbUser) async {
  logd('handleAuthStateChanges called');

  // final wasAuthenticated = fbUser != null;

  if (fbUser == null) {
    logd('fbUser is null during handleAuthStateChanges');
    _updateCachedState(false);
    _hasAuthStateChangeListenerRunAtLeastOnce = true;

    // Emit to the stream
    _isLoggedInStreamController.add(false);

    // Complete the auth state completer if it's waiting
    if (_authStateCompleter != null && !_authStateCompleter!.isCompleted) {
      _authStateCompleter!.complete(false);
    }

    signOut(useFbAuthAlso: false);
  } else {
    logd('fbUser is NOT null during handleAuthStateChanges');
    _updateCachedState(true);
    _hasAuthStateChangeListenerRunAtLeastOnce = true;

    // Emit to the stream
    _isLoggedInStreamController.add(true);

    // Complete the auth state completer if it's waiting
    if (_authStateCompleter != null && !_authStateCompleter!.isCompleted) {
      _authStateCompleter!.complete(true);
    }

    // Call all onAuthenticated callbacks by priority
    // Using a generous timeout for auth callbacks since they may need to make
    // network calls (e.g., device registration)
    await _executeCallbacksByPriority<String?>(
      _onAuthenticatedByPriority,
      fbUser.uid,
      const Duration(seconds: 30),
      'onAuthenticated',
    );
  }
}