Better Auth Skill
Better Auth is comprehensive, framework-agnostic authentication/authorization framework for TypeScript with built-in email/password, social OAuth, and powerful plugin ecosystem for advanced features.
When to Use
- Implementing auth in TypeScript/JavaScript applications
- Adding email/password or social OAuth authentication
- Setting up 2FA, passkeys, magic links, advanced auth features
- Building multi-tenant apps with organization support
- Managing sessions and user lifecycle
- Working with any framework (Next.js, Nuxt, SvelteKit, Remix, Astro, Hono, Express, etc.)
Quick Start
Installation
npm install better-auth
# or pnpm/yarn/bun add better-auth
Environment Setup
Create .env:
BETTER_AUTH_SECRET=<generated-secret-32-chars-min>
BETTER_AUTH_URL=http://localhost:3000
Basic Server Setup
Create auth.ts (root, lib/, utils/, or under src/app/server/):
import { betterAuth } from "better-auth";
export const auth = betterAuth({
database: {
// See references/database-integration.md
},
emailAndPassword: {
enabled: true,
autoSignIn: true
},
socialProviders: {
github: {
clientId: process.env.GITHUB_CLIENT_ID!,
clientSecret: process.env.GITHUB_CLIENT_SECRET!,
}
}
});
Database Schema
npx @better-auth/cli generate # Generate schema/migrations
npx @better-auth/cli migrate # Apply migrations (Kysely only)
Mount API Handler
Next.js App Router:
// app/api/auth/[...all]/route.ts
import { auth } from "@/lib/auth";
import { toNextJsHandler } from "better-auth/next-js";
export const { POST, GET } = toNextJsHandler(auth);
Other frameworks: See references/email-password-auth.md#framework-setup
Client Setup
Create auth-client.ts:
import { createAuthClient } from "better-auth/client";
export const authClient = createAuthClient({
baseURL: process.env.NEXT_PUBLIC_BETTER_AUTH_URL || "http://localhost:3000"
});
Basic Usage
// Sign up
await authClient.signUp.email({
email: "user@example.com",
password: "secure123",
name: "John Doe"
});
// Sign in
await authClient.signIn.email({
email: "user@example.com",
password: "secure123"
});
// OAuth
await authClient.signIn.social({ provider: "github" });
// Session
const { data: session } = authClient.useSession(); // React/Vue/Svelte
const { data: session } = await authClient.getSession(); // Vanilla JS
Feature Selection Matrix
Auth Method Selection Guide
Choose Email/Password when:
- Building standard web app with traditional auth
- Need full control over user credentials
- Targeting users who prefer email-based accounts
Choose OAuth when:
- Want quick signup with minimal friction
- Users already have social accounts
- Need access to social profile data
Choose Passkeys when:
- Want passwordless experience
- Targeting modern browsers/devices
- Security is top priority
Choose Magic Link when:
- Want passwordless without WebAuthn complexity
- Targeting email-first users
- Need temporary access links
Combine Multiple Methods when:
- Want flexibility for different user preferences
- Building enterprise apps with various auth requirements
- Need progressive enhancement (start simple, add more options)
Core Architecture
Better Auth uses client-server architecture:
- Server (
better-auth): Handles auth logic, database ops, API routes
- Client (
better-auth/client): Provides hooks/methods for frontend
- Plugins: Extend both server/client functionality
Implementation Checklist
Reference Documentation
Core Authentication
Advanced Features
- Advanced Features - 2FA/MFA, passkeys, magic links, organizations, rate limiting, session management
Scripts
scripts/better_auth_init.py - Initialize Better Auth configuration with interactive setup
Resources
1---
2name: better-auth
3description: Implement authentication and authorization with Better Auth - a framework-agnostic
4 TypeScript authentication framework. Features include email/password authentication
5 with verification, OAuth providers (Google, GitHub, Discord, etc.), two-factor authentication
6 (TOTP, SMS), passkeys/WebAuthn suppo...
7license: MIT
8version: 2.0.0
9risk: safe
10source: community
11metadata:
12 source: https://skillmd.com/skills/jantoniofc/better-auth
13---
14
15# Better Auth Skill
16
17Better Auth is comprehensive, framework-agnostic authentication/authorization framework for TypeScript with built-in email/password, social OAuth, and powerful plugin ecosystem for advanced features.
18
19## When to Use
20
21- Implementing auth in TypeScript/JavaScript applications
22- Adding email/password or social OAuth authentication
23- Setting up 2FA, passkeys, magic links, advanced auth features
24- Building multi-tenant apps with organization support
25- Managing sessions and user lifecycle
26- Working with any framework (Next.js, Nuxt, SvelteKit, Remix, Astro, Hono, Express, etc.)
27
28## Quick Start
29
30### Installation
31
32```bash
33npm install better-auth
34# or pnpm/yarn/bun add better-auth
35```
36
37### Environment Setup
38
39Create `.env`:
40```env
41BETTER_AUTH_SECRET=<generated-secret-32-chars-min>
42BETTER_AUTH_URL=http://localhost:3000
43```
44
45### Basic Server Setup
46
47Create `auth.ts` (root, lib/, utils/, or under src/app/server/):
48
49```ts
50import { betterAuth } from "better-auth";
51
52export const auth = betterAuth({
53 database: {
54 // See references/database-integration.md
55 },
56 emailAndPassword: {
57 enabled: true,
58 autoSignIn: true
59 },
60 socialProviders: {
61 github: {
62 clientId: process.env.GITHUB_CLIENT_ID!,
63 clientSecret: process.env.GITHUB_CLIENT_SECRET!,
64 }
65 }
66});
67```
68
69### Database Schema
70
71```bash
72npx @better-auth/cli generate # Generate schema/migrations
73npx @better-auth/cli migrate # Apply migrations (Kysely only)
74```
75
76### Mount API Handler
77
78**Next.js App Router:**
79```ts
80// app/api/auth/[...all]/route.ts
81import { auth } from "@/lib/auth";
82import { toNextJsHandler } from "better-auth/next-js";
83
84export const { POST, GET } = toNextJsHandler(auth);
85```
86
87**Other frameworks:** See references/email-password-auth.md#framework-setup
88
89### Client Setup
90
91Create `auth-client.ts`:
92
93```ts
94import { createAuthClient } from "better-auth/client";
95
96export const authClient = createAuthClient({
97 baseURL: process.env.NEXT_PUBLIC_BETTER_AUTH_URL || "http://localhost:3000"
98});
99```
100
101### Basic Usage
102
103```ts
104// Sign up
105await authClient.signUp.email({
106 email: "user@example.com",
107 password: "secure123",
108 name: "John Doe"
109});
110
111// Sign in
112await authClient.signIn.email({
113 email: "user@example.com",
114 password: "secure123"
115});
116
117// OAuth
118await authClient.signIn.social({ provider: "github" });
119
120// Session
121const { data: session } = authClient.useSession(); // React/Vue/Svelte
122const { data: session } = await authClient.getSession(); // Vanilla JS
123```
124
125## Feature Selection Matrix
126
127| Feature | Plugin Required | Use Case | Reference |
128|---------|----------------|----------|-----------|
129| Email/Password | No (built-in) | Basic auth | [email-password-auth.md](./references/email-password-auth.md) |
130| OAuth (GitHub, Google, etc.) | No (built-in) | Social login | [oauth-providers.md](./references/oauth-providers.md) |
131| Email Verification | No (built-in) | Verify email addresses | [email-password-auth.md](./references/email-password-auth.md#email-verification) |
132| Password Reset | No (built-in) | Forgot password flow | [email-password-auth.md](./references/email-password-auth.md#password-reset) |
133| Two-Factor Auth (2FA/TOTP) | Yes (`twoFactor`) | Enhanced security | [advanced-features.md](./references/advanced-features.md#two-factor-authentication) |
134| Passkeys/WebAuthn | Yes (`passkey`) | Passwordless auth | [advanced-features.md](./references/advanced-features.md#passkeys-webauthn) |
135| Magic Link | Yes (`magicLink`) | Email-based login | [advanced-features.md](./references/advanced-features.md#magic-link) |
136| Username Auth | Yes (`username`) | Username login | [email-password-auth.md](./references/email-password-auth.md#username-authentication) |
137| Organizations/Multi-tenant | Yes (`organization`) | Team/org features | [advanced-features.md](./references/advanced-features.md#organizations) |
138| Rate Limiting | No (built-in) | Prevent abuse | [advanced-features.md](./references/advanced-features.md#rate-limiting) |
139| Session Management | No (built-in) | User sessions | [advanced-features.md](./references/advanced-features.md#session-management) |
140
141## Auth Method Selection Guide
142
143**Choose Email/Password when:**
144- Building standard web app with traditional auth
145- Need full control over user credentials
146- Targeting users who prefer email-based accounts
147
148**Choose OAuth when:**
149- Want quick signup with minimal friction
150- Users already have social accounts
151- Need access to social profile data
152
153**Choose Passkeys when:**
154- Want passwordless experience
155- Targeting modern browsers/devices
156- Security is top priority
157
158**Choose Magic Link when:**
159- Want passwordless without WebAuthn complexity
160- Targeting email-first users
161- Need temporary access links
162
163**Combine Multiple Methods when:**
164- Want flexibility for different user preferences
165- Building enterprise apps with various auth requirements
166- Need progressive enhancement (start simple, add more options)
167
168## Core Architecture
169
170Better Auth uses client-server architecture:
1711. **Server** (`better-auth`): Handles auth logic, database ops, API routes
1722. **Client** (`better-auth/client`): Provides hooks/methods for frontend
1733. **Plugins**: Extend both server/client functionality
174
175## Implementation Checklist
176
177- [ ] Install `better-auth` package
178- [ ] Set environment variables (SECRET, URL)
179- [ ] Create auth server instance with database config
180- [ ] Run schema migration (`npx @better-auth/cli generate`)
181- [ ] Mount API handler in framework
182- [ ] Create client instance
183- [ ] Implement sign-up/sign-in UI
184- [ ] Add session management to components
185- [ ] Set up protected routes/middleware
186- [ ] Add plugins as needed (regenerate schema after)
187- [ ] Test complete auth flow
188- [ ] Configure email sending (verification/reset)
189- [ ] Enable rate limiting for production
190- [ ] Set up error handling
191
192## Reference Documentation
193
194### Core Authentication
195- [Email/Password Authentication](./references/email-password-auth.md) - Email/password setup, verification, password reset, username auth
196- [OAuth Providers](./references/oauth-providers.md) - Social login setup, provider configuration, token management
197- [Database Integration](./references/database-integration.md) - Database adapters, schema setup, migrations
198
199### Advanced Features
200- [Advanced Features](./references/advanced-features.md) - 2FA/MFA, passkeys, magic links, organizations, rate limiting, session management
201
202## Scripts
203
204- `scripts/better_auth_init.py` - Initialize Better Auth configuration with interactive setup
205
206## Resources
207
208- Docs: https://www.better-auth.com/docs
209- GitHub: https://github.com/better-auth/better-auth
210- Plugins: https://www.better-auth.com/docs/plugins
211- Examples: https://www.better-auth.com/docs/examples