Scope and caching
The guard protects one thing: a route handler wrapped in defineBffHandler does not run for an unverified user. Anything that answers before that handler is outside its reach.
What the guard does not prevent
The guard stops two things: people who are not signed in calling your BFF, and other websites using a signed-in user's browser to call it.
It does not stop a signed-in user from calling the BFF directly, for example with curl and their own Cookie or token. Outside a browser, Origin and Sec-Fetch-Site can be set to anything, and Bearer clients are allowed on purpose. No server-side check can tell your own frontend from a client the user controls with the same credentials.
On a site without users, the same holds for scripts that copy the Sec-Fetch-Site header your pages send.
If you need to make that harder:
- Verify an app attestation or bot-check token in
verifyUserand throw 403 without it, for example Firebase App Check, reCAPTCHA Enterprise or Cloudflare Turnstile. This raises the cost of scripting; it is not a guarantee. - Limit the damage: rate limits, and per-record authorization so that a user can do nothing through the API that the screens would not let them do.
Runs before the guard
Nitro runs every middleware before any route handler: first your own server/middleware, then middleware that modules add, such as nuxt-security, nuxt-csurf or Clerk. Then the router picks the handler.
- Your middleware runs for every request, before the guard. Do not attach tokens or forward requests upstream there; do it in the handler after the guard.
routeRules.proxyforwards the request without running your handler. Protect such a path at its own entry point, or replace the proxy with a handler that calls upstream.- Security middleware such as rate limiters and size limits answer first too, which is what you want.
Caches
Do not combine protected routes with Nitro's cache or swr route rules, defineCachedEventHandler, or a CDN cache.
- Inside Nitro's cache wrapper, the handler sees only the request headers listed in
varies. Without them,verifyUsergets no Cookie orAuthorizationand every request answers 401. - Nitro 2.14 refuses to store responses marked
privateorno-store, and the guard setsCache-Control: private, no-store. If your handler overwrites that header with a cacheable value, later requests get the stored body and the guard does not run. - A CDN in front of the app can serve a stored response before the request reaches Nitro at all.
Public data that is the same for everyone can use a separate public route with its own cache. See Caching public data.
Server-side calls
SSR and server-side $fetch calls to your own API go through the same guard. Forward the user's Cookie with useRequestFetch(), or better, call a server function directly instead of an HTTP route. See SSR and server functions.
Logging
The guard logs every denial with path, method and status, never query strings or header values. Keep credentials out of URL paths. In development it also warns about protected requests that carry neither a Cookie nor a Bearer token.