AuthServiceImpl constructor

AuthServiceImpl({
  1. required FirebaseApp firebaseApp,
  2. Future<void> onAuthenticated(
    1. String? uid
    )?,
  3. Future<void> onLoggedOut()?,
  4. Future<void> onAboutToLogOut()?,
  5. List<Future<void> Function(String? uid)>? onAuthenticatedCallbacks,
  6. List<Future<void> Function()>? onLoggedOutCallbacks,
  7. List<Future<void> Function()>? onAboutToLogOutCallbacks,
  8. List<PrioritizedCallback<Future<void> Function(String? uid)>>? onAuthenticatedPrioritized,
  9. List<PrioritizedCallback<Future<void> Function()>>? onLoggedOutPrioritized,
  10. List<PrioritizedCallback<Future<void> Function()>>? onAboutToLogOutPrioritized,
})

Implementation

AuthServiceImpl({
  required FirebaseApp firebaseApp,
  // Single callback convenience (default priority 0)
  Future<void> Function(String? uid)? onAuthenticated,
  Future<void> Function()? onLoggedOut,
  Future<void> Function()? onAboutToLogOut,
  // List of callbacks (default priority 0)
  List<Future<void> Function(String? uid)>? onAuthenticatedCallbacks,
  List<Future<void> Function()>? onLoggedOutCallbacks,
  List<Future<void> Function()>? onAboutToLogOutCallbacks,
  // Prioritized callbacks for ordered execution
  List<PrioritizedCallback<Future<void> Function(String? uid)>>?
      onAuthenticatedPrioritized,
  List<PrioritizedCallback<Future<void> Function()>>? onLoggedOutPrioritized,
  List<PrioritizedCallback<Future<void> Function()>>?
      onAboutToLogOutPrioritized,
}) {
  // This can be disabled for hardcoding
  logd('Instantiated AuthServiceImpl');
  _fbAuth = fb_auth.FirebaseAuth.instanceFor(app: firebaseApp);

  // IMPORTANT: Register ALL constructor-provided lifecycle callbacks BEFORE
  // attaching auth listeners.
  //
  // RATIONALE:
  // When a user is already logged in (warm start), Firebase can emit an auth
  // state event almost immediately after `authStateChanges().listen(...)`.
  // If callbacks are registered after listeners, the first auth event can be
  // missed by the consuming app (race condition).

  // Register prioritized callbacks first (explicit priority)
  if (onAuthenticatedPrioritized != null) {
    for (final pc in onAuthenticatedPrioritized) {
      addOnAuthenticatedCallback(pc.callback, priority: pc.priority);
    }
  }
  if (onLoggedOutPrioritized != null) {
    for (final pc in onLoggedOutPrioritized) {
      addOnLoggedOutCallback(pc.callback, priority: pc.priority);
    }
  }
  if (onAboutToLogOutPrioritized != null) {
    for (final pc in onAboutToLogOutPrioritized) {
      addOnAboutToLogOutCallback(pc.callback, priority: pc.priority);
    }
  }

  // Register list callbacks (default priority 0)
  if (onAuthenticatedCallbacks != null) {
    for (final callback in onAuthenticatedCallbacks) {
      addOnAuthenticatedCallback(callback);
    }
  }
  if (onLoggedOutCallbacks != null) {
    for (final callback in onLoggedOutCallbacks) {
      addOnLoggedOutCallback(callback);
    }
  }
  if (onAboutToLogOutCallbacks != null) {
    for (final callback in onAboutToLogOutCallbacks) {
      addOnAboutToLogOutCallback(callback);
    }
  }

  // Register single callbacks (default priority 0)
  if (onAuthenticated != null) {
    addOnAuthenticatedCallback(onAuthenticated);
  }
  if (onLoggedOut != null) {
    addOnLoggedOutCallback(onLoggedOut);
  }
  if (onAboutToLogOut != null) {
    addOnAboutToLogOutCallback(onAboutToLogOut);
  }

  // Attach listeners AFTER callbacks are registered.
  _fbAuth.authStateChanges().listen(handleAuthStateChanges);
  _fbAuth.idTokenChanges().listen((event) => handleTokenChanges(event));

  // For debuggin
  if (AppConfigBase.signoutOnReload) {
    // _fbAuth.signOut();
    signOut();
  }
}