connectToAuthService abstract method

Future<void> connectToAuthService({
  1. AuthServiceInt? authService,
  2. Future<void> onAuthenticated(
    1. String? uid
    )?,
  3. Future<void> onAboutToLogOut()?,
})

Connects this DeviceService to AuthService for automatic lifecycle wiring.

Note: This is the legacy initialization approach. For new code, prefer DreamicServices.initialize or the race-free initialize + constructor callback pattern described in the class documentation.

This method may miss auth events on warm start (when user is already logged in) because callbacks are registered after Firebase listeners have already attached. See docs/plans/auth-race/plan.auth-race.md.

Registers callbacks with AuthService to automatically:

When to Use This Method

Use this method only if:

  • Your app always requires fresh login (no warm start race condition)
  • You need to connect services dynamically after auth is established
  • You're migrating legacy code incrementally

Parameters

  • authService: Optional explicit AuthService instance. If null, attempts to resolve from GetIt. If not registered, logs and no-ops.
  • onAuthenticated: Optional override for the authenticated callback. Default: calls registerDevice().
  • onAboutToLogOut: Optional override for the pre-logout callback. Default: calls unregisterDevice().

Idempotency

Safe to call multiple times. Removes old callbacks before adding new ones.

Example (Legacy Pattern)

// Legacy setup (may miss auth events on warm start)
await deviceService.connectToAuthService();

// With explicit AuthService
await deviceService.connectToAuthService(authService: myAuthService);

// With custom callbacks
await deviceService.connectToAuthService(
  onAuthenticated: (uid) async {
    await deviceService.registerDevice();
    await analytics.trackLogin(uid);
  },
);

Implementation

Future<void> connectToAuthService({
  AuthServiceInt? authService,
  Future<void> Function(String? uid)? onAuthenticated,
  Future<void> Function()? onAboutToLogOut,
});