Create an API route
Create a catch-all server route atserver/api/auth/[...all].ts. Use Nuxt’s defineEventHandler and toWebRequest helpers to bridge the Nitro request into Better Auth’s web-standard handler.
server/api/auth/[...all].ts
You can change the base path in your Better Auth configuration, but
/api/auth is the recommended default.Create a client
Create an auth client instance and export it from a shared module. Import frombetter-auth/vue to get Vue-specific reactive helpers.
lib/auth-client.ts
useSession composable
TheuseSession composable returns a reactive reference to the current session. Use it directly in component <script setup> blocks.
components/UserMenu.vue
SSR usage
When using Nuxt SSR, passuseFetch to useSession so the session is fetched on the server and hydrated on the client without an extra round-trip.
pages/index.vue
Server-side session access
Theauth.api object exposes every Better Auth endpoint as a callable function. Access the session from any server route by passing the event headers.
server/api/me.ts
Route middleware
Use the auth client inside a Nuxt route middleware to protect pages. CallinguseSession with useFetch ensures the session check runs on the server during SSR.
middleware/auth.global.ts
Resources
- Nuxt and NuxtHub example on GitHub
- NuxtZzle — Nuxt + Drizzle ORM example on GitHub
- Nuxt example on StackBlitz
- NuxSaaS — full-stack SaaS starter kit
- NuxtOne — Nuxt starter for AIaaS applications