* refactor: rename delegated auth user table * feat: scaffold hosted better auth setup * feat: add hosted auth flows * refactor: scope project access to organizations * fix: harden hosted auth entry points * fix: stabilize org backfills and auth state * refactor: simplify hosted organization setup * fix: restore hosted auth signup flow * fix: preserve hosted workspace access * fix: preserve hosted auth redirects * Improve hosted auth UX: auto-redirect to sign-up, hide header on auth pages, add form placeholders, and trust portless dev origins - Auto-redirect unauthenticated users to /sign-up in hosted mode - Hide top nav on /sign-in and /sign-up for a cleaner auth experience - Add input placeholders across sign-in and sign-up forms - Make name field optional on sign-up (falls back to email username) - Update copy: remove 'hosted' from user-facing text, rename link to 'Create account' - Trust *.open-seo.localhost:1355 in dev mode to fix Better Auth origin rejection with portless worktrees * Simplify hosted auth flow and remove standalone PSI Use TanStack Form for sign-in and sign-up, make hosted unauthenticated handling redirect-focused, and inline auth route errors. Remove the leftover standalone PSI route, services, and table so PSI only exists within site audits. * Align project auth with Better Auth organizations * Make server function auth middleware global * Reduce auth server function boilerplate * delete migrations * fix regenerated migration data backfills * Simplify hosted auth flow and project audit scoping * Use active project context for audit actions * Allow hosted session project updates * Let agent dev server inherit auth mode * Match hosted header to gateway account menu * Scope project session updates to active project * Inline authenticated server function setup * Polish header project and account controls * restore auth generate script * Use explicit project access in server functions Make project-scoped server functions take projectId input and enforce ownership through shared middleware instead of session-backed current project state. Document the tradeoffs in an ADR so future changes can follow the same boundary. * fix ci dependency detection for auth tooling * Harden project auth in server middleware Authorize projectId automatically in authenticated server middleware and add a requireProject guard for project-scoped handlers. This makes the auth boundary harder to bypass and removes ad hoc non-null assertions from server functions. * Inline project id input schemas Remove tiny shared projectId schema helpers where they were adding indirection without reducing real complexity. Keep project-scoped validation explicit at each server function boundary. * Skip hosted backlinks access checks * Simplify auth mode helpers * Avoid rerunning auth server middleware * Simplify server function scoping ADR * Fix backlinks project scoping in hosted auth * Refine auth route foundations * Simplify ensure user auth resolution Split auth-mode context resolvers into focused modules so the middleware reads as request orchestration instead of implementation details. Reuse a shared ensured-user context type across server middleware. * Simplify hosted organization bootstrap Use Better Auth to own hosted organization creation and membership so hosted auth only needs to resolve a default active organization. Keep delegated-mode compatibility records isolated in a separate helper. * Clarify hosted auth and backlinks behavior Document the hosted AUTH_MODE deploy contract and explain why hosted deployments skip manual backlinks verification. This makes the platform-managed behavior explicit in the code paths that differ from self-serve mode. * Document hosted org creation callback Explain why auth.ts injects createOrganization into the hosted org helper. This makes the dependency direction explicit and avoids future import cycles while keeping the helper reusable. * Fix CI check failures * Fix nav link prop forwarding * save
2.1 KiB
Project scoping for server functions
Status
Accepted
Context
TanStack Start server functions do not know which route called them, so they cannot infer the active project from the URL.
We used to keep the selected project in session state and read it indirectly from middleware. That made project scope implicit and caused drift between the current page, the current request, and other open tabs.
Decision
Project-scoped server functions must accept projectId in their input.
Global server-function middleware now always resolves the authenticated user and organization. If the payload includes projectId, that same global middleware loads the project for the current organization and adds it to server-function context.
Function-level middleware is still used for type narrowing:
requireAuthenticatedContextguarantees authenticated context is present.requireProjectContextguaranteescontext.projectis present for project-scoped handlers.
In practice:
organizationIdcomes from global middleware.projectIdcomes from explicit input.context.projectexists only when the request included a validprojectId.- handlers use
context.project.id, not session-backed current-project state.
Rationale
Explicit projectId matches how server functions actually work: the request payload, not the route, defines the target resource.
This gives us:
- request-level project scope
- correct multi-tab behavior
- authorization tied to the current request
- simpler, more testable handlers
Consequences
- Project-scoped server functions should validate
projectIdin input and userequireProjectContext. - Organization-scoped server functions should use authenticated context only.
- Global middleware is the single place that resolves auth, organization, and optional project context.
- The session is no longer the source of truth for the selected project.
- Hosted mode may still apply product-level feature defaults after project access is resolved. For example, backlinks access can be treated as platform-enabled in hosted deployments, but the request must still be scoped to a project first.