Sites without users

Protect the BFF of a corporate site, a landing page or any app without sign-in.

Every file in server/api is a public HTTP endpoint. On a site without accounts, that includes the route that sends your contact form by mail or the one that calls a paid search API with your key. Anyone can call them, from any website or from a script.

Set access: 'visitor' and those routes answer only to your own pages.

nuxt.config.ts
export default defineNuxtConfig({
  modules: ['@kurashina-design/nuxt-bff-guard'],
  bffGuard: { access: 'visitor' },
})
server/api/contact.post.ts
import { defineBffHandler } from '#bff-guard'

export default defineBffHandler(async (event) => {
  await sendMail(await readValidatedBody(event, contactSchema))
  return { ok: true }
})

No verifyUser, no secret, no Cookie. The build-time check works the same way: a route under server/api without defineBffHandler fails the build.

What a visitor route checks

Browsers add Sec-Fetch-Site to every request. A visitor route accepts only same-origin, the value a fetch() from your own pages carries, for every method, reads included.

RequestSec-Fetch-SiteVisitor route
$fetch('/api/contact') from your pagesame-origin✅ handler runs
JavaScript on another websitecross-site403
A sibling subdomainsame-site403
URL typed into the address barnone403
curl without the headerabsent403

The handler does not run for a refused request, so a refused call costs nothing upstream. All current browsers send Sec-Fetch-Site; very old ones, such as Safari before 16.4, do not and are refused.

What it cannot stop

A script can send Sec-Fetch-Site: same-origin itself. Without accounts there is nothing else to tell your page from a copy of its requests. For routes that send mail or spend money, add:

Data loaded during SSR

Data that a page fetches during server-side rendering is not something to protect: anyone can open the page and read it. Mark such routes public.

A visitor route would behave inconsistently there. During SSR, useFetch forwards the headers of the page request, including Sec-Fetch-Site:

How the visitor opened the pageSSR call to a visitor route
Reloaded a page of your sitepasses (same-origin)
Came from a search engine or another site403 (cross-site)
Typed the URL or used a bookmark403 (none)
server/api/news.get.ts
import { defineBffHandler } from '#bff-guard'

// Loaded by pages/index.vue with useFetch during SSR.
export default defineBffHandler(() => listNews(), { access: 'public' })

In development, the guard logs every request a visitor route refuses, with its Sec-Fetch-Site, so such SSR calls show up while you test.

Mixing with user routes

access in nuxt.config.ts is only the default. Each route can choose:

defineBffHandler(handler, { access: 'user' })    // verifyUser must accept the request
defineBffHandler(handler, { access: 'visitor' }) // only fetches from your own pages
defineBffHandler(handler, { access: 'public' })  // anyone; writes still need your origin

A member app keeps the default 'user' and marks its marketing contact form 'visitor'. A site without accounts uses 'visitor' as the default and needs no verifyUser at all.