Configuration

All options of the bffGuard key.
nuxt.config.ts
export default defineNuxtConfig({
  modules: ['@kurashina-design/nuxt-bff-guard'],
  bffGuard: {
    access: 'user',
    verifyUser: './server/auth/verify-user.ts',
    routePatterns: ['server/api/**'],
    writeMethods: ['POST', 'PUT', 'PATCH', 'DELETE'],
  },
})
OptionDefaultDescription
access'user'Default protection of every route: 'user' or 'visitor'. See Sites without users.
verifyUsernoneServer file whose default export is the verifyUser function. Relative to the project root, or absolute. Required in production when access is 'user'.
routePatterns['server/api/**']Globs of route files that must use defineBffHandler. Your array replaces the default. See Build-time route check.
writeMethods['POST', 'PUT', 'PATCH', 'DELETE']Methods that need the same-origin check.

defineBffHandler

import { defineBffHandler } from '#bff-guard'

defineBffHandler(handler, { access?: 'user' | 'visitor' | 'public' })
OptionDefaultDescription
accessthe access option'user': verifyUser and the same-origin check for writes. 'visitor': only fetches from your own pages (Sec-Fetch-Site: same-origin), every method. 'public': no user check; writes still need your origin. Cache-Control applies to all.

Responses

StatusWhen
401verifyUser is missing, returned something invalid, or threw a 4xx other than 403.
403verifyUser threw 403, a write failed the same-origin check, or a visitor route got anything but a fetch from your own pages.
503verifyUser threw an error without a 4xx statusCode.

Every response from a wrapped handler gets Cache-Control: private, no-store.

Startup errors

  • verifyUser is required in production: set verifyUser, or access: 'visitor' if the site has no accounts.
  • Legacy configuration detected: remove apiPrefix, publicPaths and internalRequests from an older version. See Migration.
  • Nitro 2 is required: the module does not support Nitro 3 yet.