Nuxt 4 module · MIT

Nuxt server routes are open to anyone. Close them with one wrapper.

Wrap your server routes in defineBffHandler. Apps with accounts check users with the auth they already have; sites without accounts answer only to their own pages. A route that forgets the wrapper fails the build.
// 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 auth
    One 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, too
    A 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 build
    Every file under server/api must default-export defineBffHandler(...). A missing wrapper stops the build and names the file.
  • Same-origin writes
    POST, PUT, PATCH and DELETE with Cookies need Sec-Fetch-Site or Origin from your site. Verified Bearer clients skip the check.
  • Public routes are explicit
    Health checks and sign-in entry points say { access: 'public' } on the route itself, where code review can see it.
  • Outages are not sign-outs
    Invalid credentials answer 401 or 403. An auth backend that is down answers 503, so clients do not sign everyone out.

Try a request

Pick a request and see what the guard answers. The page runs the module's own decision functions in your browser.
POST /api/orders HTTP/1.1
Host: app.example
Cookie: session=…
Sec-Fetch-Site: same-origin
Origin: https://app.example
Response200
  1. Rate limiter

    nuxt-security middleware, before any route handler.

  2. verifyUser

    Your auth check. Only on user routes.

  3. Same-origin check

    Write methods only. Skipped after a verified Bearer token.

  4. Your handler

    Runs only when every step above passed.

The user was verified and the handler ran.

One layer among several

The guard does one job: no route handler runs for an unverified user. Combine it with the tools that own the other layers.
  1. CDN and reverse proxy Your hosting

    TLS, overwrite X-Forwarded-Host and X-Forwarded-Proto. Never cache protected API responses here.

  2. Your Nitro middleware server/middleware

    Runs for every request, before module middleware and the guard. Keep upstream calls and token forwarding out of it.

  3. 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.

  4. defineBffHandler Nuxt BFF Guard

    verifyUser, the same-origin check for writes, Cache-Control: private, no-store. Checked at build time.

  5. 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

The module is small, so most of the work is in how you combine it. Each recipe shows complete files for a site without users, an auth provider, a security module or a design pattern.
Sites without users
Contact forms, paid API proxies and client-side data on sites without accounts.
4 recipes
Auth providers
verifyUser for nuxt-auth-utils, Auth.js, Supabase, Clerk, Firebase and plain JWT.
6 recipes
Security modules
Rate limits, headers, CSP, CSRF tokens, bot checks and CORS next to the guard.
5 recipes
BFF patterns
Sign-in routes, webhooks, upstream proxies, roles, SSR calls and more.
10 recipes

Install

npm install @kurashina-design/nuxt-bff-guard
Kurashina Design

About Kurashina Design

Nuxt BFF Guard is an open-source project by Kurashina Design, whose brand concept is 「暮らし + いいな」.