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;
    // Clear the delivered-uid so a later addOnAuthenticatedCallback does NOT
    // replay onAuthenticated while logged out (Issue 99).
    _lastDeliveredAuthUid = null;

    // 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;
    // Carry the uid from the DELIVERED event (not currentFbUser) so a later
    // addOnAuthenticatedCallback replays with the correct uid (Issue 99).
    _lastDeliveredAuthUid = fbUser.uid;

    // 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',
    );
  }
}