Nuxt server routes are open to anyone. Close them with one wrapper.
// server/api/orders.get.ts
import { defineBffHandler } from '#bff-guard'
export default defineBffHandler(async (event) => {
// Runs only after verifyUser accepted the request.
return await listOrdersForUser(event)
})What it does
- Bring your own authOne verifyUser function calls the session or token check you already have: nuxt-auth-utils, Supabase, Clerk, Firebase, Auth.js or your own JWT.
- Sites without users, tooA corporate site's contact form or a search box that calls a paid API: one line, access: 'visitor', and only fetches from your own pages reach the handler.
- Forgotten routes fail the buildEvery file under server/api must default-export defineBffHandler(...). A missing wrapper stops the build and names the file.
- Same-origin writesPOST, PUT, PATCH and DELETE with Cookies need Sec-Fetch-Site or Origin from your site. Verified Bearer clients skip the check.
- Public routes are explicitHealth checks and sign-in entry points say { access: 'public' } on the route itself, where code review can see it.
- Outages are not sign-outsInvalid credentials answer 401 or 403. An auth backend that is down answers 503, so clients do not sign everyone out.
Try a request
POST /api/orders HTTP/1.1
Host: app.example
Cookie: session=…
Sec-Fetch-Site: same-origin
Origin: https://app.exampleRate limiter
nuxt-security middleware, before any route handler.
verifyUser
Your auth check. Only on user routes.
Same-origin check
Write methods only. Skipped after a verified Bearer token.
Your handler
Runs only when every step above passed.
The user was verified and the handler ran.
One layer among several
CDN and reverse proxy Your hosting
TLS, overwrite X-Forwarded-Host and X-Forwarded-Proto. Never cache protected API responses here.
Your Nitro middleware server/middleware
Runs for every request, before module middleware and the guard. Keep upstream calls and token forwarding out of it.
Security middleware nuxt-security, nuxt-csurf
Headers and CSP, rate limits, request size limits, CORS for other origins. Module middleware runs after yours and before any route handler.
defineBffHandler Nuxt BFF Guard
verifyUser, the same-origin check for writes, Cache-Control: private, no-store. Checked at build time.
Your route handler server/api
Per-record authorization (403), calls to upstream APIs with the user's token, the actual work.
Recipes for real stacks
Install
npm install @kurashina-design/nuxt-bff-guard