BFF patterns
Per-record authorization
event.contextcreateError
The guard says who the user is. The handler decides what they may touch.
verifyUser answers "is this a signed-in user of the app?". It cannot know whether that user may read order 42. That check belongs in the handler, right next to the query.
server/auth/verify-user.ts
import type { VerifyUser } from '@kurashina-design/nuxt-bff-guard'
export default (async (event) => {
const { user } = await requireUserSession(event) // nuxt-auth-utils, or your own
event.context.user = { id: user.id, tenantId: user.tenantId }
return { viaBearer: false }
}) satisfies VerifyUser
server/api/orders/[id].get.ts
import { defineBffHandler } from '#bff-guard'
export default defineBffHandler(async (event) => {
const user = event.context.user!
const id = getRouterParam(event, 'id')!
// Scope the query to the user's tenant instead of loading first and checking after.
const order = await db.order.findFirst({ where: { id, tenantId: user.tenantId } })
// 404, not 403: do not reveal that the order exists in another tenant.
if (!order) throw createError({ statusCode: 404 })
return order
})
server/types/context.d.ts
declare module 'h3' {
interface H3EventContext {
user?: { id: string, tenantId: string }
}
}
// nuxt-auth-utils: the fields your session stores for each user.
declare module '#auth-utils' {
interface User {
id: string
tenantId: string
}
}
export {}
Errors that your handler throws pass through the guard unchanged: the 401/403/503 mapping applies only to verifyUser.