Better Auth Nuxt

A Nuxt module for simple, flexible authentication in your Nuxt applications.
Features
- ⚙️ Auto-scanning of client and server configs for end-to-end TS support
- 🚀 Built-in authentication middleware
- 🔑 Multiple authentication strategies (username, email, ...)
- 🛡️ Role-based access control
- 🔄 Session management
- 🔒 Redirect handling for authenticated/unauthenticated users
Quick Setup
npm install better-auth-nuxt better-auth
yarn add better-auth-nuxt better-auth
pnpm add better-auth-nuxt better-auth
- Add the module to your
nuxt.config.ts:
export default defineNuxtConfig({
modules: ['better-auth-nuxt'],
betterAuth: {
endpoint: '/api/auth/**',
redirectOptions: {
redirectGuestTo: '/auth/login',
redirectUserTo: '/',
redirectUnauthorizedTo: '/401',
},
options: {
client: {
basePath: '/api/auth',
},
server: {
appName: 'My Nuxt App',
},
},
}
})
- Use the module in your pages:
<script setup>
// Protect route for authenticated users only
definePageMeta({
auth: {
only: 'user',
}
})
// Access auth functionality
const { loggedIn, user, signOut } = useUserSession()
</script>
<template>
<div v-if="loggedIn">
<h1>Welcome, {{ user?.name }}</h1>
<button @click="signOut()">Sign Out</button>
</div>
</template>
Module Options
Auth Configuration
interface ModuleOptions {
endpoint: string
serverConfigs?: string[]
clientConfigs?: string[]
options: {
client?: ModuleClientOptions
server?: ModuleServerOptions
}
redirectOptions: {
redirectUserTo?: string
redirectGuestTo?: string
redirectUnauthorizedTo?: string
}
}
Server Options
interface ModuleServerOptions {
appName?: string
baseURL?: string
basePath?: string
secret?: string
}
Client Options
interface ModuleClientOptions {
baseURL?: string
basePath?: string
disableDefaultFetchPlugins?: boolean
}
API Reference
Client-side Composables
useUserSession()
Provides access to the authenticated user session and auth methods.
const {
loggedIn,
user,
session,
fetchSession,
signIn: {
username,
email,
},
signUp: {
username,
email,
},
signOut,
options,
} = useUserSession()
Server-side Utilities
useAuth()
Access the auth instance on the server.
const auth = useAuth()
Route Protection
Use the auth meta property to protect routes:
definePageMeta({
auth: {
only: 'user' | 'admin' | 'guest' | ['user', 'admin'],
redirectUserTo: '/dashboard',
redirectGuestTo: '/login',
redirectUnauthorizedTo: '/unauthorized',
}
})
Configuration Files
You can create configuration files to customize authentication:
Server Configuration
Create a *.better-auth.ts file to configure server-side auth:
import type { BetterAuthOptions } from 'better-auth'
export default () => ({
} satisfies BetterAuthOptions)
Client Configuration
Create a *.better-auth-client.ts file to configure client-side auth:
import type { ClientOptions } from 'better-auth'
export default {
} satisfies ClientOptions
Note: After adding or modifying configuration files, run pnpm nuxt prepare to ensure your changes are properly recognized by Nuxt.
Contribution
Local development
pnpm install
pnpm dev:prepare
pnpm dev
pnpm dev:build
pnpm lint
pnpm test
pnpm test:watch
pnpm release