connectToAuthService abstract method
Future<void>
connectToAuthService({
- AuthServiceInt? authService,
- Future<
void> onAuthenticated(- String? uid
- 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:
- Call registerDevice on authentication (login)
- Call unregisterDevice before logout (while still authenticated)
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: callsregisterDevice().onAboutToLogOut: Optional override for the pre-logout callback. Default: callsunregisterDevice().
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,
});