AuthServiceImpl constructor
AuthServiceImpl({
- required FirebaseApp firebaseApp,
- Future<
void> onAuthenticated(- String? uid
- Future<
void> onLoggedOut()?, - Future<
void> onAboutToLogOut()?, - List<
Future< ? onAuthenticatedCallbacks,void> Function(String? uid)> - List<
Future< ? onLoggedOutCallbacks,void> Function()> - List<
Future< ? onAboutToLogOutCallbacks,void> Function()> - List<
PrioritizedCallback< ? onAuthenticatedPrioritized,Future< >void> Function(String? uid)> - List<
PrioritizedCallback< ? onLoggedOutPrioritized,Future< >void> Function()> - List<
PrioritizedCallback< ? onAboutToLogOutPrioritized,Future< >void> Function()>
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();
}
}