Cloudflare Access in Front of /admin

Optional hardening: put a Cloudflare Access application in front of /admin on your own Cloudflare account, so only your staff's verified identities reach the admin, on top of the app's own checks.

This is optional. The admin is already closed to everyone but platform admins: every page and action checks user.role with requireAdmin and answers 404 to anyone else (overview). Cloudflare Access adds a second lock in front of the app, so a visitor who isn't one of your staff never reaches /admin at all. It runs in your own Cloudflare account, with no change to the app or the deploy.

Access Never Replaces the App's Guard

Keep both. Access decides who may reach the page; the app still decides who is an admin. Behind Access, requireAdmin, user.role and the 404 stay exactly as they are: a staff member Access lets through, who isn't a platform admin, still gets the app's 404. Never remove or loosen an app check because Access is on, and never use Access to skip the 404.

Before You Start

  • The app must be served on a custom domain on a zone in your Cloudflare account (for example app.acme.com). A Cloudflare Access application protects a hostname and paths. A workers.dev address has only a per-Worker Access switch, which covers the whole Worker, not /admin alone.
  • A Cloudflare One (Zero Trust) account. Access has a free plan for small teams.
  • You configure everything in your own dashboard. This guide needs no API token, and none should go in .env, a chat or a commit.

Setup

Menu names follow the Cloudflare One dashboard as of this writing; Cloudflare renames them now and then, so if one isn't there, search for it in the dashboard.

  1. Choose how staff prove who they are. Cloudflare One → Integrations → Identity providers → Add new. Pick the provider your staff already use (Google Workspace, Microsoft Entra ID, GitHub, Okta), or use One-time PIN, which emails a code to an allowed address and needs no provider.
  2. Create the application. Access controls → Applications → Add an application → Self-hosted.
  3. Name it (for example Admin) and set the session duration (how long a staff member stays signed in to Access before the login page returns). A working day is a common choice. See Limits for what an expired session does.
  4. Add the destinations, two for the same hostname, so the page and everything under it are covered:
    • hostname app.acme.com, path /admin
    • hostname app.acme.com, path /admin/*
  5. Add an Allow policy. In the application's policies, add a policy with the Allow action and an Include rule that lists your staff: Emails (named addresses), Emails ending in (your company domain), or an Access group you maintain. Anyone who matches no Allow policy is refused.
  6. Pick the identity providers the application accepts (the ones from step 1) and save.

Use the same addresses staff use to sign in to the app's own account: the app still checks that account's user.role.

After Setup, Check

Three checks, each in a browser:

  1. A non-staff browser gets the Access login. In a private window with no staff session, open https://app.acme.com/admin. You see Cloudflare Access's sign-in page, not the app's 404. An address that isn't on the policy is refused there.
  2. A staff non-admin still gets the app's 404. Sign in to Access with a staff address whose app account isn't a platform admin, then open /admin. Access lets you through and the app answers 404.
  3. Navigation inside /admin works. As a staff platform admin (first admin), open /admin, then Users, a user and Organizations. Ban a test user, confirm the dialog and watch the page refresh. Every page and action stays under /admin, so one application covers them.

Limits

  • Localized paths. If your app serves more than one language, /<locale>/admin is a different path from /admin. Add the same two destinations for each locale in apps/web/config/i18n.config.ts (/it/admin, /it/admin/*, and so on), or a localized address reaches the admin without Access. The app's own checks still apply there.
  • Local development isn't behind Access. pnpm dev and pnpm preview run on your computer, never through Cloudflare. Test the app's own guard locally; test Access on your real domain.
  • Only /admin is covered. The user's own app (/dashboard, /settings), /api/auth (sign-in) and the impersonation Stop button (/impersonation) aren't under /admin, so they stay open to everyone, as they must: an admin who impersonates lands in the user's app, and Stop is posted there.
  • An expired Access session breaks in-app navigation. The admin moves between pages without a full reload, and a request that Access answers with its login redirect can't complete from the page. If a click suddenly does nothing, reload the page: Access shows its login and you continue. A longer session duration makes this rarer.
  • Access is not a replacement for two-factor. Keep admin two-factor on: Access proves who reached the page, not that the app account is protected.