Introducing a new way to review your PRs.

Read the announcement

calicastle / cali.so

Personal website for Cali Castle

View on GitHub

21 documents · Updated Sep 21, 2026

docs / admin-shell-and-access-control

Admin shell and access control

The admin area is a private operating space for the site owner. It uses Clerk for sign-in, checks ownership again on the server before page data or admin actions run, and presents AMA, media, and photo work through one small dashboard shell.

Access boundary

Request flow

The access boundary has two layers. The proxy stops unauthenticated navigation early, while page loaders and API handlers confirm that the signed-in user is the site owner before they read or change private data.

flowchart TD
    A[Request to /admin or /api/admin] --> B{Admin page?}
    B -->|Yes| C[Clerk proxy checks sign-in]
    B -->|No| D[Admin API handler checks owner access]
    C -->|Signed out| E[Sign-in redirect or fail-closed response]
    C -->|Signed in| F[Page loader checks site-owner access]
    F -->|Not the owner| G[Forbidden response]
    F -->|Owner| H[Protected page data and shell]
    D --> I{Owner access}
    I -->|Unauthenticated| J[401 response]
    I -->|Forbidden| K[403 response]
    I -->|Authorized| L[Read or mutate admin data]

The proxy in proxy.ts applies Clerk to /admin pages and /api/admin routes. It calls auth.protect() for page navigation. API routes still need their own authorization check because the proxy does not replace handler-level access control. Development-only AMA fixture pages return not found outside development before they reach the sign-in boundary.

References: proxy.ts• L26-43 proxy.ts• L61-72 proxy.ts• L74-82

Login handoff

The admin login route is a redirect boundary rather than a login form. app/…/page.tsx calls the owner page guard with /admin as the return path. A signed-out visitor goes to the Clerk sign-in page, then returns to the admin overview; an authorized owner goes straight to /admin.

References: app/…/page.tsx• L1-12 lib/…/server.ts• L30-40

Owner authorization

Owner access result

Owner authorization turns the current Clerk session into one of three outcomes: authorized, unauthenticated, or forbidden. This distinction lets page requests redirect people who have not signed in while giving a clear denial to signed-in users who are not the owner.

An authorized result carries an owner principal. The principal has a stable owner data identity for data access and the Clerk user ID for rate limits and audit records. The authorizer requires all of the following:

  • An authenticated session.
  • An active session state.
  • A current Clerk user whose public metadata has the exact marker siteOwner: 'yes'.

Email address and client-writable metadata do not grant owner access. This keeps the permission tied to a server-read identity marker rather than to a value that a user can change or that can be shared by more than one account.

References: lib/…/authorization.ts• L1-45 lib/…/authorization.test.ts• L58-131

Page protection

The protected layout adds the admin shell and runs a second owner check in parallel with the page content. Each page loader also calls the same guard before it loads its own data. The repeated checks form defense in depth and prevent a page from touching owner data when only the visual shell has been prepared.

requireOwnerPage() returns the principal for an owner. It sends a forbidden result to the framework for a signed-in non-owner. It sends an unauthenticated request to Clerk with the requested return path.

References: app/…/layout.tsx• L1-23 app/…/page.tsx• L56-78 app/…/page.tsx• L54-69 lib/…/server.ts• L28-40

API protection

Admin API handlers use ownerRequestAuthenticator as their common access check. After authorization, handlers pass the principal's owner data identity to the service layer and use the actor ID for privileged action records. This keeps the same owner rule across AMA, media, photo selection, and sign-out actions.

Read requests need owner authorization. Browser mutations also require a same-origin browser request and pass through an admin mutation limit. Privileged actions record a pseudonymous actor ID so the system can trace actions without placing the raw Clerk user ID in the audit record.

References: lib/…/server.ts• L42-51 lib/…/http.ts• L183-217 lib/…/http.ts• L78-115 lib/…/service.ts• L116-180

Client-safe failure responses

Session probe

The public preferences panel uses /api/admin/session as a small owner probe. It runs only when the panel opens, so public pages do not need an admin request during normal page load. The probe returns { owner: false } for visitors, non-owners, and authentication-provider errors. It never redirects and does not expose the reason for a denial to public UI.

The response is not cached. A later panel open can correct a stale client hint after a sign-in, sign-out, or provider recovery.

References: app/…/route.ts• L1-21 components/preferences.tsx• L62-87 components/preferences.tsx• L193-207

Admin API denials

Admin API failures use small, stable responses that clients can handle without receiving provider or server detail. The common outcomes are:

ConditionResponseClient meaning
No authorized owner session401 with unauthorizedSign-in or refresh the session.
Signed-in user lacks owner access403 with forbiddenDo not retry as the current user.
Auth or service dependency fails503 with dependency_unavailableKeep private state unchanged and try later.
Browser mutation is not same-origin403 with no bodyReject the request before the action runs.
Mutation limit is reached429 with retry informationWait before sending the action again.

Responses use no-store and no-referrer controls. Mutation handlers also record denied authentication, blocked browser requests, rate limits, and successful privileged actions. AMA form flows use a safe redirect back to the settings page when a form action fails rather than showing provider details.

References: lib/…/http.ts• L44-85 lib/…/http.ts• L183-217 lib/…/http.ts• L78-115 lib/…/http.ts• L243-343 lib/…/request-policy.ts• L7-40

Operational dashboard shell

Static shell

The admin root layout in app/…/layout.tsx provides the non-public document and Clerk provider. The document can be prepared as a static shell while Clerk refreshes the short-lived session cookie in the browser. Private values do not enter this shell; they stream from guarded page loaders.

The protected layout places the content in AdminShell and keeps the owner dock visible while page data loads. Each page supplies its own loading shape so navigation feels immediate without treating a loading shell as proof of access.

References: app/…/layout.tsx• L11-35 app/…/layout.tsx• L7-23 app/…/AdminShell.tsx• L1-19

The owner dock is the main admin navigation. It links to the overview, AMA, Media, and Photos areas. It marks the current surface from the route, offers a return path to the public site, and places sign-out in the admin Preferences panel.

The overview is an operational summary rather than a data store. It points the owner toward work that needs attention and shows the next AMA Session, new time requests, media counts, and photo draft or publication counts. AMA then groups booking work and settings into their own pages; deeper pages use a back link to preserve that hierarchy.

References: components/admin-dock.tsx• L20-24 components/admin-dock.tsx• L83-153 components/admin-nav.tsx• L3-61 app/…/AdminOverview.tsx• L69-155 app/…/page.tsx• L141-187

Sign-out action

Sign-out is an owner-only mutation from the admin Preferences panel. The route checks browser request policy and owner access, applies the admin mutation limit, revokes the active Clerk session, records the successful action, and redirects to the public site without caching the result.

References: components/preferences.tsx• L208-215 app/…/route.ts• L1-20 lib/…/logout.ts• L28-85

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep proxy page protection, page-loader checks, and API authenticator checks aligned. A new admin route must not rely on the visual shell as its only boundary.
  • Preserve the three-way owner result. Client probes need a safe false answer, page requests need sign-in redirects for unauthenticated users, and API clients need distinct 401 and 403 outcomes.
  • Keep the owner data identity and actor ID roles separate when a handler passes authorization results to a service or audit recorder.
  • If the Clerk provider or its rendering mode changes, re-check both session-cookie refresh behavior and the static admin shell assumption.
  • When adding a mutating admin endpoint, apply same-origin checks, mutation limits, no-store denial headers, and privileged-action audit records through the shared policy.
  • When adding a dock surface, update route activity, fallback markup, and the overview or menu links as one navigation contract.

Important failure modes

TriggerConsequenceGuard
A signed-out request reaches an admin pageThe page is not shown and the visitor is sent to sign-in or receives a fail-closed response.Clerk protection in proxy.ts and requireOwnerPage().
A signed-in non-owner requests an admin page or APIPrivate content is denied.Exact server-side siteOwner: 'yes' authorization.
An admin mutation omits the canonical origin or same-origin fetch markerThe action is rejected before the service runs.Shared browser mutation policy.
The auth provider or rate limiter is unavailableThe client receives a safe retryable response and no privileged action runs.503 handling and mutation security service.
The public session probe failsThe Admin row stays hidden; public chrome does not show an auth error.{ owner: false } fallback and no-store response.
Development-only AMA fixtures are requested outside developmentThe fixture surface is unavailable.Proxy rewrite to the not-found route.

Key files

FileWhy to read it
proxy.tsDefines which admin requests enter Clerk and how the outer boundary fails closed.
lib/…/authorization.tsDefines the owner decision and the principal passed to protected work.
lib/…/server.tsConnects Clerk to page guards and the shared API authenticator.
app/…/layout.tsxShows the non-public document and session-refresh provider boundary.
app/…/layout.tsxShows how the protected shell and defense-in-depth page check fit together.
app/…/AdminShell.tsxShows the common content column and owner dock frame.
components/admin-dock.tsxDefines the operational navigation surfaces and active route behavior.
app/…/route.tsDefines the client-safe owner probe used by public preferences.
lib/…/http.tsIllustrates the shared owner, browser, rate-limit, and JSON response boundary for admin APIs.
lib/…/http.tsIllustrates protected browser form flows and safe redirects for AMA settings and calendar actions.
lib/…/logout.tsDefines the protected session revocation flow and its safe completion response.
Administration and accessParent page for the wider administration and access model.