arch-atlas

Auth State Notifier

core/services/auth_storage.dart · StateNotifier<AuthState>

domain

Overview

Global StateNotifier exposing the (isLoggedIn, role) tuple. On construction it inspects persisted tokens and resolves the initial role: seller token present → freelancer, customer-only → client, otherwise guest.

User Flow

  1. App boot → AuthStateNotifier ctor calls _loadInitialState.
  2. _loadInitialState reads isLoggedIn from SharedPreferences and inspects hasSellerRole().
  3. switchRole(newRole) is invoked when the user toggles between client and freelancer in the UI — only succeeds if a token for that role exists.
  4. logout() clears the session, deletes the FCM token, and resets state to AuthState(false, guest).

Cases & Edge Cases

  • AuthState overrides == and hashCode so refreshAuthState() does not re-trigger watchers when the (isLoggedIn, role) tuple is unchanged — without this, /store/customers/me would fetch twice on home boot (see explicit comment in code).
  • logout swallows NotificationService.deleteToken errors when FCM is not initialized.
  • switchRole silently no-ops if the requested role has no stored token — the UI must not assume the call succeeded.

Code References

  • lib/src/core/services/auth_storage.dart:257 // authStateProvider
  • lib/src/core/services/auth_storage.dart:263 // AuthStateNotifier
  • lib/src/core/services/auth_storage.dart:328 // AuthState equality override

API Calls

Empty — fill this in later. Open the manifest and add content here.

Notes

Lives in auth_storage.dart alongside the storage service rather than under features/auth — it is consumed across the whole app (settings, profile, navigation) so its DI scope is core.