Bits
auth.email-code — Someone signs in by typing the code from the email
- Trigger: "Sign in with the code" on the "Check your email" screen of
/sign-in, after asking for a sign-in email (auth.email-link). For phones: the link opens in whatever browser the mail app picks, the code signs in where the person asked. - Who: anyone who received the email.
- Path:
apps/app/src/app/(site)/sign-in/sign-in-form.tsx(CheckEmail) →authClient.signIn.emailOtp→POST /auth/sign-in/email-otp→ Better Auth email-otp plugin (apps/api/src/auth/auth.ts) → newsessionrow and cookie → a full page load of the page they came from (/meby default). That page comes from?next=and is checked bysafeNext(apps/app/src/lib/safe-next.ts): a path on this site, or/me. - Data: reads and deletes the code's row in
verification(identifiersign-in-otp-<email>, a hash of the code and the tries so far, 15 minutes); writesuserthe first time, andsession. - Rules: spec 0006. The code is created with the link, in the same email (
createVerificationOTPinsidesendMagicLink); only the latest email's code works, and it works once. 3 wrong tries spend it. Stored hashed. Never in the subject or the preview. Signing in with it creates the account like the link and syncs the crew role fromCREW_EMAILS. It only ever lands on this site:?next=is parsed as a URL and kept only if it stays on our origin, because browsers read/\evil.comas//evil.com(spec 0010). The plugin's other routes are closed (disabledPaths, 404). - Errors: a wrong code says so and lets them retry; an expired or spent code offers "Email me a new one"; Better Auth answers 429 after 3 requests a minute.
- Test:
e2e/auth.email-code.e2e.ts(a new visitor signs in with the code, "123 456" works, a wrong code says so, a used code doesn't work again, a crafted?next=lands on/me);apps/api/src/auth/sign-in-code.test.ts;apps/app/src/lib/safe-next.test.ts;apps/app/e2e/forms.spec.ts(focus and hints) - Observe: API log lines with
bit: "auth.sign-in"; locally the email, with its code, is printed by[Mail]. - Status: verified — 2026-11-18 (end-to-end test)