# Sign-in and email

## Emailed codes, no passwords
- `POST /api/auth/start {email}` → make a 6-digit code, store only `sha256(email + ":" + code)`, expiry 15 minutes, send it.
- `POST /api/auth/verify {email, code}` → accept **any** unexpired code for that email (emails arrive late; people tap "send again"), count wrong tries across all of them (max 5), then expire them all.
- Rate-limit starts per email and per IP (e.g. 5 an hour). Store attempts in a table and clean it up.
- Session: random token in an `HttpOnly; Secure; SameSite=Lax` cookie, only its hash in the database, renewed on use, revocable from Settings.
- Local development: show the code on screen instead of emailing (`DEV_CODES=1` in `.dev.vars`), never on the live site.
- Why two emails arrive: double-taps and retries. Debounce the button and say "We've sent a code" with a countdown before "Send again".

## Families and teams
- An invite key or link (short, readable, e.g. `blue-ivy-yew-quill-4485`), single-use or limited uses with an expiry. Roles (parent / family / member) checked on the server for every request.
- Rotating the key signs everyone else out: say so before doing it.

## Sending email
- Cloudflare Email Service via a `send_email` binding. Needs the user's own domain on Cloudflare (SPF/DKIM records are added for them).
- **Sending to anyone other than the account's verified addresses needs Workers Paid ($5/month).** Say so and let the user decide before building features that depend on it.
- Send a test to the user first. Ask before the first email to any real person.
- Plain-text plus simple HTML; the code or key fact in the subject and first line. Reply-to the user's normal inbox.
- Anything not essential (summaries, news) gets an unsubscribe link that works in one tap.

## Notifications (web push)
See `phone-features.md` for the full set-up and the lessons learned. In short: VAPID keys as secrets (never regenerate), a Cron Trigger, ask permission only after a tap, quiet hours, and say plainly that delivery is best effort.
