Move (app) pages and server actions onto the principal [ABD-21]

Merged
FR
6d ago
+643-504

What & why

Fourth step of ABD-21 (one request principal). #720–#722 put every API route behind authedRoute; this does the same for (app) pages and server actions.

  • requireCompletedSignup() returns the principal. Pages write const { user } = await requireCompletedSignup(); instead of looking the session up again with getAuthUser / getRequestUserWithMfa (dashboard, rooms, save, settings tokens/import/export/invites), and drop the signed-out branches the gate already redirects. app-page-gate.test.ts accepts that form as the first statement, with a test that a principal declared after a query still fails.
  • authedAction (@/lib/auth/authed-action), the server-action counterpart of authedRoute: runs the action in a viewer frame for the cookie user (2FA and completed signup enforced) and returns the action's own unauthorized result without running it. Server actions previously never ran in a viewer frame, so RLS never applied to them.
  • Wrapped: changeUsername, deleteAccount, requestEmailChange (settings) and deleteUserAsAdmin, reprocessIssueGroupAsAdmin (admin, admin check kept inside).
  • Under the restricted role the caller sees and updates only its own users row, so the username/email-taken checks read on systemRead and deleting the account row uses systemDb. settings/actions.ts is added to the systemDb allowlist in biome.json. changeUsername writes only columns granted to abode_app.
  • authed-action.test.ts covers the wrapper and fails for any (app) action exported another way (only the reviewed signOut re-export is allowed).
  • Agent guidance (AGENTS.md, app/src/app/AGENTS.md) updated.

Thread: https://ampcode.com/threads/T-01a1e700-7a74-76b5-85a8-6c3a6faaad6c

Checklist

  • User-facing change → added/updated a PostHog event (and a funnel/insight for a new flow), or N/A — N/A, internal refactor
  • User-facing or service change → docs updated (README features/roadmap/services, .env.example, in-app help, CONTRIBUTING.md, onboarding checklist), or N/A — N/A for user docs; agent guidance updated as above
  • New error paths report via captureServerException / the error boundary, or N/A — N/A, no new error paths
  • New behavior is covered by tests, or N/A — authed-action.test.ts (wrapper contract + gate), app-page-gate.test.ts extended; existing settings/admin action tests pass through the wrapper
  • User-facing change → demo video attached (record-pr-demo skill); refactor through a user flow → parity run, or N/A — N/A, auth plumbing only; E2E covers the flows
  • If this fixes a recurring defect, considered a guardrail via the learn skill, or N/A — N/A, not a defect fix; the action gate is the new guardrail

Notes

Verified locally:

  • bun run check:fix and lint clean; full unit suite passes (4,470); integration project 659/659 (placeholder Supabase env values).
  • The action gate fails for an unwrapped export const and for a plain export async function (both checked).
  • Each commit type-checks on its own.

Not done here: (auth) actions (login, signup, reset) are pre-sign-in by design and unchanged; admin pages keep requireAdmin. Remaining ABD-21 steps: an explicit asSystem entry point replacing the systemDb allowlist, and RLS_ENFORCED everywhere.


Summary by cubic

Server actions and (app) pages now resolve the caller as a single principal instead of looking the session up per file, and (app) server actions get the same gate API routes got.

  • requireCompletedSignup() now returns the principal; pages write const { user } = await requireCompletedSignup(); and drop the signed-out branches the gate already redirects.
  • authedAction runs each action in a viewer frame with 2FA and completed signup enforced, returning the action's unauthorized result without running it when there's no caller; authed-action.test.ts requires it on every (app) action except the reviewed signOut re-export.
  • Cross-user checks (username/email taken) read on systemRead and account-row deletion uses systemDb, since the restricted role sees only its own row; settings/actions.ts joins the systemDb allowlist.
  • Admin actions keep their admin check inside the wrapped action.