Move (app) pages and server actions onto the principal [ABD-21]
Merged
FR
6d ago+643+643+012345678901234567890123456789-504-504-012345678901234567890123456789
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 writeconst { user } = await requireCompletedSignup();instead of looking the session up again withgetAuthUser/getRequestUserWithMfa(dashboard, rooms, save, settings tokens/import/export/invites), and drop the signed-out branches the gate already redirects.app-page-gate.test.tsaccepts 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 ofauthedRoute: 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) anddeleteUserAsAdmin,reprocessIssueGroupAsAdmin(admin, admin check kept inside). - Under the restricted role the caller sees and updates only its own
usersrow, so the username/email-taken checks read onsystemReadand deleting the account row usessystemDb.settings/actions.tsis added to thesystemDballowlist inbiome.json.changeUsernamewrites only columns granted toabode_app. authed-action.test.tscovers the wrapper and fails for any(app)action exported another way (only the reviewedsignOutre-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.tsextended; existing settings/admin action tests pass through the wrapper - User-facing change → demo video attached (
record-pr-demoskill); 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
learnskill, or N/A — N/A, not a defect fix; the action gate is the new guardrail
Notes
Verified locally:
bun run check:fixand lint clean; full unit suite passes (4,470); integration project 659/659 (placeholder Supabase env values).- The action gate fails for an unwrapped
export constand for a plainexport 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 writeconst { user } = await requireCompletedSignup();and drop the signed-out branches the gate already redirects.authedActionruns each action in a viewer frame with 2FA and completed signup enforced, returning the action'sunauthorizedresult without running it when there's no caller;authed-action.test.tsrequires it on every(app)action except the reviewedsignOutre-export.- Cross-user checks (username/email taken) read on
systemReadand account-row deletion usessystemDb, since the restricted role sees only its own row;settings/actions.tsjoins thesystemDballowlist. - Admin actions keep their admin check inside the wrapped action.