nuxt-better-auth-utils is a Nuxt module providing a full lifecycle integration for the Better Auth library, offering auto-wired API handlers, SSR session pre-fetching, and typed server/client APIs. It's currently at version 0.1.27 and has a rapid release cadence with frequent bug fixes and minor improvements, often multiple releases per week. Key differentiators include its tight integration with Nuxt's SSR capabilities, providing reactive session state via `useAuth()` on the client and `useServerAuth()` for server-side operations, and automatic type inference. It remains database-agnostic, deferring to Better Auth's adapter ecosystem. This module aims to simplify authentication setup in Nuxt applications by handling much of the boilerplate, including API routing, session management, and protecting pages with middleware, without imposing opinions on the underlying authentication strategy or data storage.
npm install nuxt-better-auth-utilsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the core setup of nuxt-better-auth-utils, including Nuxt module configuration, defining server and client authentication configurations, and using the `useAuth()` composable and `auth` middleware to protect a page. It illustrates integration with a hypothetical Drizzle database adapter.
Ensure `server/auth.server.config.ts` and `app/auth.client.config.ts` are correctly placed. Use `defineAuthConfig` and `defineAuthClientConfig` imported from the module's virtual aliases.
Update to the latest version of `nuxt-better-auth-utils` and ensure your Nuxt environment is up-to-date. Recheck type definitions and auto-imported functions for any conflicts, especially for custom session types or server utilities.
Always pass the `event` object received by `defineEventHandler` to `useServerAuth()` methods like `requireSession` or `getSession`.
Set `BETTER_AUTH_SECRET` in your `.env` file (e.g., `BETTER_AUTH_SECRET='super-secret-key-that-is-at-least-32-chars-long'`) or `NUXT_SECRET` for production deployments.
Upgrade to `v0.1.21` or newer. This version uses `$Infer.Session` for server auth types to mitigate the depth limit issue.
Ensure `nuxt-better-auth-utils` is correctly listed in your `nuxt.config.ts` modules array. Restart your Nuxt development server. Check for any `tsconfig.json` misconfigurations.
Add `'nuxt-better-auth-utils'` to the `modules` array in your `nuxt.config.ts`.
Use `requireSession(event)` if an authenticated user is strictly required (it throws an error for unauthenticated requests), or defensively check `if (session) { /* ... */ }` after `getSession(event)`.Set `BETTER_AUTH_SECRET` in your `.env` file (e.g., `BETTER_AUTH_SECRET='a-strong-random-secret-at-least-32-chars'`) or provide it through your hosting environment configuration.