Getting started
Two ways to use it:
- Your app has user accounts. Follow this page: connect your existing auth through
verifyUser. - Your site has no accounts (a corporate site, a landing page). See Sites without users; it is one line of configuration.
Requirements
Nuxt 4.7 with Nitro 2 on Node.js 22.19+ or 24.11+. The module is tested with Nuxt 4.7.0, Nitro 2.14.0, h3 1.15.11 and ofetch 1.5.1.
Install
npm install @kurashina-design/nuxt-bff-guard
Configure
Add the module and point verifyUser at a server file. The path is relative to the project root.
export default defineNuxtConfig({
modules: ['@kurashina-design/nuxt-bff-guard'],
bffGuard: {
verifyUser: './server/auth/verify-user.ts',
},
})
Write verifyUser
verifyUser receives the H3Event and calls the check you already have. Return { viaBearer } when the user is verified, throw a 401 when the credentials are missing or invalid.
import { createError } from 'h3'
import type { VerifyUser } from '@kurashina-design/nuxt-bff-guard'
export default (async (event) => {
const session = await getMySession(event) // your existing check
if (!session) throw createError({ statusCode: 401 })
return { viaBearer: false }
}) satisfies VerifyUser
See verifyUser for the full contract and the examples for real providers.
Wrap your routes
Import defineBffHandler from #bff-guard and default-export it from every route file under server/api.
import { defineBffHandler } from '#bff-guard'
export default defineBffHandler(async (event) => {
return await listOrdersForUser(event)
})
A request without a valid user now gets a 401 and your handler never runs. If a route file under server/api is not wrapped, nuxt build fails and lists the file.
Check it
curl -i http://localhost:3000/api/orders
# HTTP/1.1 401 Unauthorized
Then sign in through your app and confirm that the normal screens, sign-out and token refresh still work.