Build Auth Repository Session Restore Logic
epic-email-password-login-foundation-task-007 — Implement the session restoration flow in AuthRepository that reads the cached encrypted token on app launch, validates it against Supabase, and refreshes it if expired. Return a typed result (restored session, expired, or no session) so callers can route the user appropriately without a fresh login.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Define SessionRestoreResult as a Dart sealed class with four variants so the compiler enforces exhaustive handling in switch expressions. The method signature should be: Future
On refresh success, persist the new session via the storage adapter. On any other error, delete the stored token and return expired. Avoid catching broad Exception types — catch AuthException specifically so unexpected errors propagate. Keep this method free of UI concerns; the BLoC/Cubit layer is responsible for routing.
Testing Requirements
Unit tests must cover all four SessionRestoreResult branches using mocked SupabaseClient and mocked encrypted storage adapter. Verify that expired-token path calls both recoverSession() (fails) and that the result is .expired. Verify that the refresh path calls refreshSession() and writes the new token to storage. Verify the noSession fast path makes zero network calls.
All tests must run offline and complete in under 300ms total. Achieve 100% branch coverage on the restoreSession() method.
Supabase client initialization may fail silently in certain Flutter environments if environment variables are missing or the anon key is rotated, leading to runtime null-pointer errors throughout the auth layer.
Mitigation & Contingency
Mitigation: Add explicit assertion checks during app startup that verify the Supabase client is initialized before the router resolves. Document required --dart-define keys in the project README and add a CI step that validates their presence.
Contingency: Implement a fallback initialization error screen with a clear message and a retry button. Log initialization failures to crash reporting immediately.
The flutter_secure_storage package behaviour differs between iOS Keychain and Android Keystore implementations. On Android, biometric-enrolled devices may require additional authentication to read stored tokens, causing unexpected session read failures.
Mitigation & Contingency
Mitigation: Test the repository on Android devices with and without biometric enrollment early in development. Use accessibility options in flutter_secure_storage to configure whether biometric authentication is required for storage access.
Contingency: If biometric-gated storage causes regressions, fall back to a non-biometric storage option for session tokens (reserving biometric-gated storage for higher-sensitivity credentials only).