# ADR 009: Use isolated Better Auth and Google identity for Finance

- HTML version: https://robbiepalmer.me/projects/personal-finance-app/adrs/009-better-auth-google-consumer-identity
- Project: Personal Finance App (https://robbiepalmer.me/projects/personal-finance-app.md)
- Status: Accepted
- Date: 2026-12-01
- Initiatives: Digital Twins for Everyday Life (https://robbiepalmer.me/initiatives/digital-twins-for-everyday-life.md)

## Context

[ADR 001](/projects/personal-finance-app/adrs/001-household-identity-and-authorization-boundary)
separates a login from household membership and financial ownership. The shared
site already has a Better Auth deployment for Recipe Site, but Finance has its
own database, session lifecycle, authorization rules, and breach boundary.
Sharing Recipe sessions would couple unrelated user records and make identical
cookie names ambiguous on `robbiepalmer.me`.

The browser must use first-party cookies. [Better Auth documents a reverse
proxy][Better Auth cookies] as the remedy for browsers that reject cross-domain
authentication cookies, and supports a separate cookie prefix for applications
on one origin. Its [Google provider][Better Auth Google provider] also derives
the callback from the configured public URL and auth path.

## Decision

Run a separate Better Auth instance in the Personal Finance Worker and store its
users, linked provider accounts, sessions, verification records, and rate-limit
counters in the Finance PostgreSQL database. Keep the browser on the site origin
through `/api/finance/auth/*`; the Pages Function proxies that path to the
Worker. Prefix every Finance cookie with `personal-finance` so Recipe and
Finance sessions cannot shadow one another.

Use Google as the only production sign-in method. Link a returning verified
Google identity to the existing verified local email only under Better Auth's
provider checks. Do not link accounts with different emails, offer password
sign-in, or let support impersonate a user. A user recovers access through the
Google account. Loss of that identity requires a future, separately designed
recovery process; support cannot bypass membership or read household records.

[Sessions][Better Auth sessions] last seven days and refresh after one day of
use. Operations that need fresh authentication use a 15-minute freshness
window. Logout revokes the server session. The Worker accepts only the exact
public origin, uses `Secure`, `HttpOnly`, `SameSite=Lax` cookies in HTTPS
environments, and reads the client address only from Cloudflare's overwritten
`cf-connecting-ip` header.

Enable [Better Auth's limiter][Better Auth rate limiting] with PostgreSQL
storage. Apply a 100-request, 60-second default and tighter limits to social
sign-in, OAuth callback, and the preview-only email sign-in and sign-up paths.
There is no production password recovery or invitation endpoint to rate-limit.

Preview Workers may enable email/password sign-in only when both the deployment
label and canonical isolated Worker hostname match. Preview tests create
deterministic users in the preview database. They never use production OAuth
tokens, cookies, or sessions. Production disables that method regardless of
request input.

After authentication, every private route resolves the server session and then
loads current household membership. A session alone never grants access to a
household, and financial ownership never grants application access.

## Consequences

Finance gets first-party browser sessions without sharing Recipe identity or
cookies. Session revocation, rate limiting, and household authorization remain
durable across Worker isolates. Google receives identity and callback data but
no financial records.

The Google OAuth client must register the exact
`https://robbiepalmer.me/api/finance/auth/callback/google` redirect. Users who
lose their Google account have no application-managed fallback. Adding another
provider, email recovery, invitations, or support access requires a later
decision and abuse model.

Preview password auth exists solely in test infrastructure and is unavailable
as a production sign-in option.
Tests must continue proving that production cannot enable it and that preview
identities cannot reach another household.

## Alternatives considered

### Reuse the Recipe session

This was rejected because Recipe membership and Finance membership are
different security records with different databases and incident boundaries.

### Call the Worker directly from the browser

This was rejected because it creates a cross-domain cookie flow and breaks the
same-origin session rule, especially in browsers with third-party-cookie
restrictions.

### Add email and password recovery now

This was rejected because it adds password handling and an email-delivery
recovery channel before the founder needs either. Google recovery is sufficient
for the first production user; a second method needs its own threat model.

[Better Auth cookies]: https://better-auth.com/docs/concepts/cookies

[Better Auth rate limiting]: https://better-auth.com/docs/concepts/rate-limit

[Better Auth sessions]: https://better-auth.com/docs/concepts/session-management

[Better Auth Google provider]: https://better-auth.com/docs/authentication/google

---

Markdown index of this site: https://robbiepalmer.me/llms.txt
