Skip to content

[POC — do not merge] Member auth against AvyWeb (Better Auth) - #1277

Draft
busbyk wants to merge 1 commit into
busbyk/docs-updatesfrom
poc/better-auth-members
Draft

busbyk wants to merge 1 commit into
busbyk/docs-updatesfrom
poc/better-auth-members

Conversation

@busbyk

@busbyk busbyk commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Warning

Proof of concept: do not merge. This draft shows how AvyApp would sign in Members against AvyWeb (ADR 026 in the web repo), so the team can review the approach and try it locally. Nothing user-facing changes: it's one hidden screen under the Developer Menu. The server half is NWACus/web#1501.

Summary

AvyApp becomes a Better Auth client of AvyWeb's apex (avy-fx.org/api/auth/*) through @better-auth/expo:

  • Anonymous first, created lazily. On the first action that needs an identity (later: subscribing to notifications), the app creates an Anonymous Member and registers this install (installId plus a random secret, both in SecureStore; the server stores only a hash of the secret).
  • Device restore. If the session is lost, the device secret gets a new session for the same Anonymous Member, with no UI.
  • Linking. Signing in with an email code (or Google) while anonymous turns the Member into a real account. The server moves Devices, Subscriptions and Memberships over and deletes the anonymous Member. From then on the device secret no longer signs in.
  • Session. The session is a full one: the app runs no third-party embeds, unlike center websites. The expo client keeps the avy.session_token cookie in SecureStore and sends it on every request. The rolling session lasts 60 days.
sequenceDiagram
    participant App as AvyApp
    participant Apex as AvyWeb apex /api/auth
    App->>Apex: A: POST /sign-in/anonymous
    App->>Apex: POST /device/register {installId, secret}
    Note over App: session lost
    App->>Apex: B: POST /sign-in/device {installId, secret}
    Apex-->>App: same Anonymous Member, new session
    App->>Apex: C: email code (or D: Google idToken) with the anonymous session
    Apex-->>App: Linked Member (device, subscriptions and memberships moved)
Loading
File What
network/memberAuth.ts Auth client, device identity, and flows A–D
components/screens/main/MemberAuthDebug.tsx Developer Menu → Member auth (POC)
clientContext.ts avyWebHost (prod https://avy-fx.org, staging https://dev.avy-fx.org)
app.config.ts Google Sign-In config plugin, added only when GOOGLE_IOS_URL_SCHEME is set

Gotchas the POC turned up, now handled in the code:

  • expoClient must use cookiePrefix: 'avy' to match the server. Otherwise it silently never stores the session.
  • The app sends expo-origin: avy://, and the server must list avy:// in trustedOrigins. Otherwise every POST that carries a cookie returns 403 INVALID_ORIGIN.
  • expoClient sends no cookie on Google idToken sign-ins, so the app attaches the anonymous session itself. Without that, Google wouldn't link the anonymous Member. This path is untested.

Stacked on #1269 — Docs updates for local development and debugging, so the diff only shows the POC commit.

Screenshots

Testing

Tested on the iOS simulator against a local AvyWeb. Android and Google were not tested.

1. Run AvyWeb locally

Follow the setup in NWACus/web#1501 (branch busbyk/avyapp-better-auth-members, pnpm seed, pnpm dev, MEMBER_AUTH_RATE_LIMIT=false). Nothing is deployed yet: avy-fx.org and dev.avy-fx.org both return 404 for /api/auth.

2. Build the app

  1. Check out this branch, then run yarn install.
  2. In .env, set EXPO_PUBLIC_AVY_WEB_HOST=http://localhost:3000. You can also type the host on the screen.
  3. Run npx expo prebuild --clean && npx expo run:ios. A new dev build is required because this adds native modules (expo-secure-store, expo-crypto, @react-native-google-signin/google-signin). Expo Go and older dev builds will crash on the screen.
    • Android (untested): run adb reverse tcp:3000 tcp:3000 so localhost:3000 reaches your machine, then npx expo run:android.
  4. Open the side drawer → Developer Menu → Member auth (POC). The /get-session card at the bottom shows the current session as JSON after every action.

3. Walk the flows

Step Do Expect
A A: sign in anonymously /get-session shows user.isAnonymous: true, and Install id is filled in. In the web admin, Members has a new anonymous Member and Devices has a Device owned by it.
B Drop stored session (keep device), then B: restore session (relaunch) Status device, with the same Member id and a new session. (A real app restart keeps the cookie, so B just returns session.)
C Type an email, C1: send email OTP, enter the code (see the web PR for where it shows up), then C2: verify email OTP isAnonymous: false and a different Member id. In the admin, the anonymous Member is gone and the Device now belongs to the linked Member.
After C Drop stored session (keep device), then B Status linked: the device secret no longer signs in, so the app would show sign-in options.
Sign out Sign out /get-session is null. The Device stays registered.
Reset Forget device Wipes the install id, secret and session from SecureStore. Start over at A.

D: sign in with Google needs OAuth clients that don't exist yet: iOS, Android and preview-Android clients, plus EXPO_PUBLIC_GOOGLE_WEB_CLIENT_ID, EXPO_PUBLIC_GOOGLE_IOS_CLIENT_ID and GOOGLE_IOS_URL_SCHEME in the app, and the matching ids on the server. Expect it to fail until those exist.

Request Member deletion emails a confirmation link, so it needs SMTP configured on the web side.

  • Needs a new native build (not OTA-safe): native dependency, app.json, or Expo/RN version changed

🤖 Generated with Claude Code


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Better Auth expo client with SecureStore, device identity (install id + secret),
anonymous, device-secret relaunch, email OTP linking and native Google flows,
behind a Developer Menu screen. Local only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant