connectToAuthService method

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

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

@override
Future<void> connectToAuthService({
  AuthServiceInt? authService,
  Future<void> Function(String? uid)? onAuthenticated,
  Future<void> Function()? onAboutToLogOut,
}) async {
  logd('DeviceService: connectToAuthService called');

  // Resolve auth service if not provided
  AuthServiceInt? resolvedAuthService = authService;
  if (resolvedAuthService == null) {
    try {
      resolvedAuthService = g<AuthServiceInt>();
    } catch (e) {
      logw('DeviceService: AuthServiceInt not registered in GetIt, cannot connect: $e');
      return;
    }
  }

  // Remove old callbacks if already connected (idempotency)
  if (_isConnectedToAuthService && _authService != null) {
    logd('DeviceService: Removing existing callbacks before reconnecting');
    _disconnectFromAuthService();
  }

  _authService = resolvedAuthService;

  // Create callback wrappers with default behavior
  _registeredOnAuthenticatedCallback = onAuthenticated ?? _defaultOnAuthenticated;
  _registeredOnAboutToLogOutCallback = onAboutToLogOut ?? _defaultOnAboutToLogOut;

  // Register callbacks
  _authService!.addOnAuthenticatedCallback(_registeredOnAuthenticatedCallback!);
  _authService!.addOnAboutToLogOutCallback(_registeredOnAboutToLogOutCallback!);

  _isConnectedToAuthService = true;

  // Also connect to lifecycle service for automatic resume updates
  _connectToLifecycleService();

  logd('DeviceService: Connected to AuthService and lifecycle');
}