Full technical details in your project at
/docs/modules/Auth.mdWhat You Get
- ✅ 3 auth providers - Apple, Google, Email (all via Supabase)
- ✅ Session persistence - Users stay logged in automatically
- ✅ Proactive token refresh - 60s before expiry, with 3-attempt retry
- ✅ Keychain storage - Secure, encrypted token persistence
- ✅ AsyncStream state - Observable authentication state
- ✅ MockAuthClient - DEBUG mode works without backend
Key Components
AuthClient Protocol
Production Setup (From Real Code)
How authentication is set up inCompositionRoot.swift:
What SessionManager Does
On App Launch:- Loads session from Keychain
- Validates token expiry
- Auto-refreshes if expiring soon
- Emits
.authenticatedor.unauthenticatedstate
- Exchanges provider token with Supabase
- Saves access + refresh tokens to Keychain
- Schedules proactive refresh
- Emits
.authenticated(user)state
- Scheduled 60s before token expiry
- Retries up to 3 times with backoff
- Handles Supabase token rotation
- Clears session on refresh failure
- ✅ Race-safe refresh mutex
- ✅ Cancellation-aware
- ✅ Comprehensive error handling
- ✅ Fully tested (85%+ coverage)
Token Management
Automatic Refresh
Secure Storage
All tokens stored in Keychain:- ✅ Access token
- ✅ Refresh token
- ✅ Never in UserDefaults
- ✅ OS-level encryption
Auth State Observation
Customization Examples
Add Social Provider
Add Custom Fields
Mock Auth in DEBUG
Enabled by default! No setup needed.- Edit Scheme → Run → Environment Variables
- Add
AUTH_BYPASS=0 - Configure Supabase in
Config/Secrets.xcconfig
Key Files
| Component | Location |
|---|---|
| Protocol | Packages/Auth/Sources/Auth/Protocols/AuthClient.swift |
| Supabase | Packages/Auth/Sources/Auth/Supabase/ |
| Apple Sign In | Packages/Auth/Sources/Auth/Apple/ |
| Session | Packages/Auth/Sources/Auth/Session/ |
| Mock | Packages/Auth/Sources/Auth/Mock/ |
Dependencies
- Core - Error handling, logging
- Networking - HTTP client (for Supabase)
Used By
- All features - Protected by authentication
- CompositionRoot - Observes auth state
- LaunchRouter - Auth-gated navigation
Best Practices
Token Security
Token Security
- Always store in Keychain
- Never log tokens
- Refresh before expiry
- Clear on sign out
Error Handling
Error Handling
- Map to AppError
- User-friendly messages
- Retry transient failures
- Log technical details
Testing
Testing
- Use MockAuthClient
- Test token refresh
- Test error scenarios
- Test state transitions
Learn More
Full Documentation
Complete Auth guide
Supabase Setup
Configure backend
Building Guide
Customize auth flow
Architecture
See auth in system
Test Coverage
87%+ - Comprehensive auth testing Tests include:- Sign in/up flows
- Token refresh
- Session management
- Apple Sign In coordination
- Error scenarios
- State transitions
