Skip to main content
Full technical details in your project at /docs/modules/Auth.md

What You Get

  • 3 auth providers - Apple, Google, Email (all via Supabase)
  • Reliable session persistence - Users stay logged in for days, not hours
  • Automatic token refresh - Silent background refresh keeps sessions alive
  • Expired session recovery - Refresh tokens restore sessions even after access token expires
  • Keychain storage - Secure, encrypted token persistence
  • AsyncStream state - Observable authentication state (including .refreshing)
  • MockAuthClient - DEBUG mode works without backend
Time saved: 20-32 hours of auth flows, token management, session handling, and comprehensive testing.

Module structure (since v2.0)

  • SessionManager is split across four files: SessionManager.swift, SessionManager+SignIn.swift, SessionManager+Refresh.swift, and SessionManager+Persistence.swift. The public actor API is unchanged. This is the ≤ 400-line rule in action.
  • SupabaseAuthAPI helpers moved to SupabaseAuthAPI+Mapping.swift. Private helpers only; no API change.
  • All protocol-typed properties now use the explicit any keyword required by Swift 6.

Session Behavior

Users only need to re-authenticate when the refresh token expires (7+ days by default, configurable in Supabase).

Key Components

AuthClient Protocol

Production Setup (From Real Code)

How authentication is set up in CompositionRoot.swift:

What SessionManager Does

On App Launch:
  1. Loads session from Keychain
  2. Validates token expiry
  3. Auto-refreshes if expiring soon
  4. Emits .authenticated or .unauthenticated state
On Sign In:
  1. Exchanges provider token with Supabase
  2. Saves access + refresh tokens to Keychain
  3. Schedules proactive refresh
  4. Emits .authenticated(user) state
Token Refresh:
  • Scheduled 60s before token expiry
  • Recovers expired sessions using refresh token
  • Retries up to 3 times with exponential backoff
  • Handles Supabase token rotation
  • Users stay logged in for days, not hours
Production Quality:
  • ✅ 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 a Social Provider

Apple and Email ship wired out of the box. Google is implemented in the package (GoogleSignInCoordinator) but is not wired in CompositionRoot by default — pass a Google provider to SessionManager(google:) to enable it. To add another provider, declare it on AuthClient and implement it on the production client (SessionManager, in SessionManager+SignIn.swift):

Add Custom Fields

Mock Auth in DEBUG

Enabled by default! No setup needed. CompositionRoot selects the mock at startup. In DEBUG it uses MockAuthClient unless AUTH_BYPASS=0 is set; in RELEASE the mock is compiled out entirely.
To use real auth in DEBUG:
  1. Edit Scheme → Run → Environment Variables
  2. Add AUTH_BYPASS = 0
  3. Configure Supabase in Config/Secrets.xcconfig

Key Files

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

  • Always store in Keychain
  • Never log tokens
  • Refresh before expiry
  • Clear on sign out
  • Map to AppError
  • User-friendly messages
  • Retry transient failures
  • Log technical details
  • 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

85%+ - Comprehensive auth testing (run via the AuthTests target in the single Boilerplate.xctestplan pass) Tests include:
  • Sign in/up flows
  • Token refresh
  • Session management
  • Apple Sign In coordination
  • Error scenarios
  • State transitions