Auth providers

Auth.js with @sidebase/nuxt-auth

@sidebase/nuxt-authgetServerSessiongetToken
Check the Auth.js session, and accept the same JWT as a Bearer token.

@sidebase/nuxt-auth wraps Auth.js (NextAuth). On the server, getServerSession(event) from #auth returns the session or null.

server/auth/verify-user.ts
import { createError } from 'h3'
import type { VerifyUser } from '@kurashina-design/nuxt-bff-guard'
import { getServerSession } from '#auth'

export default (async (event) => {
  // null when nobody is signed in. Configuration errors throw 500 → the guard answers 503.
  const session = await getServerSession(event)
  if (!session?.user) throw createError({ statusCode: 401 })
  event.context.user = session.user
  return { viaBearer: false }
}) satisfies VerifyUser

Also accepting Bearer tokens

With the JWT session strategy, getToken({ event }) decodes the session token. It reads the session Cookie first and only falls back to Authorization: Bearer when there is no Cookie. Report viaBearer: true only in that case.

server/auth/verify-user.ts
import { createError, getHeader } from 'h3'
import type { VerifyUser } from '@kurashina-design/nuxt-bff-guard'
import { getToken } from '#auth'

const sessionCookies = ['next-auth.session-token', '__Secure-next-auth.session-token']

export default (async (event) => {
  const token = await getToken({ event }) // null when missing or invalid
  if (!token?.sub) throw createError({ statusCode: 401 })

  const cookieHeader = getHeader(event, 'cookie') ?? ''
  // Large sessions are split into chunks such as next-auth.session-token.0
  const usedCookie = sessionCookies.some(name => cookieHeader.includes(name))
  event.context.user = { id: token.sub }
  return { viaBearer: !usedCookie }
}) satisfies VerifyUser

The guard also checks the header shape itself, so viaBearer only takes effect when the request really carries a Bearer token.

The Auth.js endpoints (/api/auth/* by default) are created by the module's NuxtAuthHandler, which you write in server/api/auth/[...].ts. Mark that one file public: export default defineBffHandler(NuxtAuthHandler({ ... }), { access: 'public' }). Auth.js applies its own CSRF token to its POST endpoints. Providers that post their callback from their own site (response_mode=form_post, as Sign in with Apple does) are rejected by the same-origin check; use a redirect-based callback for them.