Migrate consumer end-user authentication from PocketBase to Better Auth against the existing PostgreSQL database (shared with PowerSync in hfw-server), retaining PocketBase exclusively as an internal headless CMS for the hymn administration team. Enable modern WebAuthn / Passkey biometric login alongside standard email/password authentication.
Architecture & Separation of Concerns
PocketBase (Internal Headless CMS): Retained solely for the content curation team to edit hymns, hymnals, categories, and media. The existing cron job continues syncing hymn content one-way from PocketBase to PostgreSQL. Consumer traffic is completely removed from PocketBase.
PostgreSQL (hfw-postgres): Single operational database hosting:
User app data (user_lists, user_list_items, user_hymn_history)
Better Auth: Handles end-user registration, password authentication, session management, and WebAuthn / Passkeys. Issues JWTs compatible with PowerSync’s JWKS verification.
PowerSync: Continues real-time replication with Flutter/Web clients, validating user tokens directly against Better Auth JWTs.
Phased Migration Plan
Phase 1: Backend Setup (Better Auth & PostgreSQL in hfw-server)
Initialize Better Auth service connected to POWERSYNC_STORAGE_CONNECTION_URI in PostgreSQL.
Configure plugins: @better-auth/passkey (RP ID hymnsforworship.app) and jwt (configured with PS_JWKS_KEY / RS256).
Implement/expose REST auth endpoints (/api/auth/* or /v1/auth/*).
Wire /api/powersync-auth token issuance to validate Better Auth sessions/JWTs.
Set up .well-known endpoints for platform passkey associations (apple-app-site-association and assetlinks.json).
Phase 2: User Data Migration
Export existing consumer users from PocketBase users collection.
Write one-time migration script to populate Better Auth user and account tables in PostgreSQL, preserving user IDs (id) and customer mappings (customer_id).
Clear consumer users from PocketBase, leaving only admin/superuser accounts.
Phase 3: Client & API Layer Updates (hfw_api, hfw_flutter, hfw_cli)
Update HFWApiClient in packages/hfw_api to target Better Auth endpoints (/api/auth/sign-in/email, /api/auth/sign-up/email, etc.).
Update AuthService in packages/hfw_flutter to support Better Auth session tokens.
Update ServerContentAccessService.verifyToken in packages/hfw_core to verify Better Auth JWTs locally via JWKS/secret instead of querying PocketBase.
Update hfw_cli to use Better Auth REST authentication.
Phase 4: Passkey & UI Integration
Add passkey Flutter package / platform bindings to packages/hfw_flutter.
Add “Sign in with Passkey” button & conditional autofill to LoginScreen.
Add “Sign up with Passkey” option to SignupScreen.
Add “Passkeys & Biometrics” management section to ProfileScreen.
Verified title adheres to Conventional Commits (feat(auth): ...)
Codebase Analysis & Touchpoints:
Token Verification & Server Entitlements (hfw_core / hfw_api):
packages/hfw_core/lib/src/services/server/server_content_access_service.dart (ServerContentAccessService.verifyToken, lines 59–74) currently sends a POST request to $pbUrl/api/collections/users/auth-refresh and extracts pbData["record"]. This should be updated to validate Better Auth JWTs / sessions against JWKS or PostgreSQL.
packages/hfw_api/lib/src/routes/users.dart (_registerUserAccessRoutes, lines 11–150) relies on ServerContentAccessService.verifyToken for /users/<id>/access and /users/<id>/hymns/<hymnId>/access, and queries $pbUrl/api/collections/users/records directly in /auth/check-email (lines 106–150).
PocketBase Reverse Proxy (hfw_api):
packages/hfw_api/lib/src/router.dart (lines 235–253) mounts a catch-all /api/ proxy forwarding directly to $pbUrl/api/. During migration, /api/auth/* should route to Better Auth and public PocketBase consumer user collection endpoints should be restricted.
Client API & Session Management (hfw_api / hfw_flutter):
packages/hfw_api/lib/src/api_client.dart (HFWApiClient) needs typed Better Auth client methods for email/password and WebAuthn passkey flows.
packages/hfw_flutter/lib/services/auth/auth_service.dart (AuthService) persists _token, _userId, _userRecord, and _savedAccounts in SharedPreferences and broadcasts token updates to PowerSync.
Current Status:0% implemented. No better-auth, passkey, or webauthn code exists in the repository. Consumer authentication remains 100% PocketBase across all services.
Expanded PocketBase Auth Footprint (new surfaces since issue creation):
In addition to the original checklist (hfw_server, hfw_core, hfw_api, hfw_flutter, hfw_cli), any Better Auth migration must now also cover:
packages/hfw_core & packages/hfw_api:
ServerContentAccessService.verifyToken (packages/hfw_core/lib/src/services/server/server_content_access_service.dart) still validates tokens via $pbUrl/api/collections/users/auth-refresh.
packages/hfw_api/lib/src/router.dart still mounts the /api/ reverse proxy to PocketBase.
packages/hfw_dl (Download Center — Astro / Cloudflare Workers):
Authenticates consumer sessions against PocketBase and queries user entitlements via src/pages/api/hfw/access.ts and src/utils/hfwAccess.ts.
Recent commits (9174c0f, 0d26fd5) added desktop web login callbacks (app_callback) for Slides for Worship and hardened session verification on top of the current PocketBase flow.
Native Mobile Clients in packages/hfw_flutter:
Alongside packages/hfw_flutter/lib/services/auth/auth_service.dart and hfw_headless_coordinator.dart, consumer login/signup/profile UIs now live in native SwiftUI (ios/Runner/HfwStore.swift) and Android Jetpack Compose (android/app/src/main/kotlin/com/appleeducate/hfw/HfwSettingsAndAuthComposeViews.kt, HfwStore.kt).
packages/hfw_cms (Internal Admin CMS):
Uses PocketBase admin auth (src/lib/auth/pocketbase.ts), which aligns with keeping PocketBase internal-only (or migrating admin auth separately later).
Recommended Next Step (Keep on Roadmap & Split):
Keep this as a P2 roadmap initiative and update Phases 3–5 to explicitly include hfw_dl, hfw_jaspr (app_callback), and native iOS/Android auth views.
Recommend splitting Phases 1–3 (Core Better Auth backend, PowerSync JWKS, user migration, and client cutover) from Phase 4 (WebAuthn / Passkeys + .well-known platform bindings) so the core auth cutover can ship and stabilize before adding passkey UX across iOS, Android, and Web.