Auth providers
nuxt-auth-utils
nuxt-auth-utilsrequireUserSession
Use the sealed Cookie session of nuxt-auth-utils as the user check.
nuxt-auth-utils keeps the session in an encrypted Cookie. Its server utilities are auto-imported, and requireUserSession throws a 401 when nobody is signed in, which the guard passes on as 401.
nuxt.config.ts
export default defineNuxtConfig({
modules: ['nuxt-auth-utils', '@kurashina-design/nuxt-bff-guard'],
bffGuard: {
verifyUser: './server/auth/verify-user.ts',
},
})
server/auth/verify-user.ts
import type { VerifyUser } from '@kurashina-design/nuxt-bff-guard'
export default (async (event) => {
// Throws statusCode 401 without a session → the guard answers 401.
const { user } = await requireUserSession(event)
event.context.user = user
// The session is a Cookie, never a Bearer token.
return { viaBearer: false }
}) satisfies VerifyUser
The routes that create and clear the session must work without one, so they are public.
server/api/auth/login.post.ts
import { defineBffHandler } from '#bff-guard'
export default defineBffHandler(async (event) => {
const { email, password } = await readBody(event)
const user = await findUserByPassword(email, password) // your own check
if (!user) throw createError({ statusCode: 401 })
await setUserSession(event, { user: { id: user.id, name: user.name } })
return { ok: true }
}, { access: 'public' })
server/api/auth/logout.post.ts
import { defineBffHandler } from '#bff-guard'
export default defineBffHandler(async (event) => {
await clearUserSession(event)
return { ok: true }
}, { access: 'public' })
Both are POST, so the guard still requires your own origin. A forged form on another site cannot sign your users in or out.
The OAuth handlers of nuxt-auth-utils (
defineOAuthGitHubEventHandler and others) usually live in server/routes/auth/, outside the default routePatterns. They redirect the browser and verify the provider's response themselves, so they do not need the guard.