nuxt-better-auth
Use when implementing auth in Nuxt apps with @onmax/nuxt-better-auth - provides client composables, server helpers, route protection, session refresh helpers, and Better Auth plugin integration.
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- nuxt-better-auth
- license
- MIT
- description
- Use when implementing auth in Nuxt apps with @onmax/nuxt-better-auth - provides client composables, server helpers, route protection, session refresh helpers, and Better Auth plugin integration.
02 资源文件 11
- 资产文件assets/README.md525 B
- 参考文档references/client-auth.md2.3 KB
- 参考文档references/client-only.md716 B
- 参考文档references/database.md991 B
- 参考文档references/installation.md1.8 KB
- 参考文档references/plugins.md1.5 KB
- 参考文档references/README.md580 B
- 参考文档references/route-protection.md1.5 KB
- 参考文档references/server-auth.md2.4 KB
- 参考文档references/types.md945 B
- 脚本scripts/README.md622 B
03 SKILL.md 正文
Nuxt Better Auth
Authentication module for Nuxt 4+ built on Better Auth. It adds Nuxt-specific setup, route protection, server helpers, and typed auth state.
Alpha status: the package is still pre-stable. Verify behavior against the current docs and source before relying on edge cases.
When to Use
- Installing/configuring
@onmax/nuxt-better-auth - Implementing sign-in, sign-up, sign-out, or custom auth flows
- Protecting routes (client and server)
- Accessing user session in API routes
- Refreshing session state after custom auth endpoints or server-side session changes
- Integrating Better Auth plugins (admin, passkey, 2FA)
- Setting up database with NuxtHub
- Using clientOnly mode for external auth backends
Available Guidance
| File | Topics |
|---|---|
| references/installation.md | install flow, env vars, config files |
| references/client-auth.md | useUserSession, client methods, redirects, loading states, custom actions |
| references/server-auth.md | serverAuth, session helpers, refreshSessionCookieCache, API enforcement |
| references/route-protection.md | route rules, page meta, API protection |
| references/plugins.md | plugin pairing between server and client |
| references/database.md | NuxtHub schema generation, secondary storage |
| references/client-only.md | external Better Auth backends and clientOnly mode |
| references/types.md | public auth types and augmentation |
Usage Pattern
- Installing module? → references/installation.md
- Login/signup forms? → references/client-auth.md
- API route protection? → references/server-auth.md
- Route rules/page meta? → references/route-protection.md
- Using plugins? → references/plugins.md
- Database setup? → references/database.md
- External auth backend? → references/client-only.md
- TypeScript types? → references/types.md
Do not load every reference file by default. Pick the smallest file that matches the task.
Key Concepts
| Concept | Description |
|---|---|
useUserSession() |
Client composable - user, session, loggedIn, refresh, sign-out |
runWithSessionRefresh() |
Refresh local session after custom auth endpoints |
requireUserSession() |
Server helper - throws 401/403 if not authenticated |
refreshSessionCookieCache() |
Refresh Better Auth's cached session cookie after server updates |
auth route mode |
'user', 'guest', { user: {...} }, or false |
serverAuth() |
Get Better Auth instance in server routes |
Quick Reference
// Client: useUserSession()
const { user, loggedIn, signIn, signOut } = useUserSession()
await signIn.email({ email, password }, { onSuccess: () => navigateTo('/') })
// Client: custom auth endpoint
await runWithSessionRefresh(() => $fetch('/api/custom-login', { method: 'POST', body }))
// Server: requireUserSession()
const { user } = await requireUserSession(event, { user: { role: 'admin' } })
// Server: after updating fields returned by session helpers
await updateCurrentUser(event)
await refreshSessionCookieCache(event)
// nuxt.config.ts: Route protection
routeRules: {
'/admin/**': { auth: { user: { role: 'admin' } } },
'/login': { auth: 'guest' },
'/app/**': { auth: 'user' }
}
Broad rules such as '/**': { auth: 'user' } skip framework/module internals like /_nuxt/**, /_ipx/**, /api/auth/**, /api/_better-auth/**, and /api/_nuxt_icon/**.
Resources
04 同仓库更多 Skills
web-perf
Audit, diagnose, or optimize website loading and interaction performance, Core Web Vitals, and Lighthouse performance scores.
unlighthouse
Run Google Lighthouse on every page of a site with the unlighthouse CLI, and fail CI on low scores with unlighthouse-ci. Use when a task mentions unlighthouse, unlighthouse-ci, site-wide Lighthouse scans, performance budgets in CI, unlighthouse.config.ts, defineUnlighthouseConfig, scanner.exclude, dynamic sampling, the authenticate hook, or a scan that never exits, skips pages, or refuses to clear its output folder.
performance-optimization
Optimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks.
fixing-accessibility
Audit and fix HTML accessibility issues including ARIA labels, keyboard navigation, focus management, color contrast, and form errors. Use when adding interactive controls, forms, dialogs, or reviewing WCAG compliance.
better-accessibility
Reviews and fixes keyboard and focus behavior, ARIA, accessible names, forms, screen-reader announcements, motion and zoom in your project against WCAG 2.2.
web-perf
Audit, diagnose, or optimize website loading and interaction performance, Core Web Vitals, and Lighthouse performance scores.