Authentication Mastery (Better Auth)
Framework-agnostic TypeScript authentication with Better Auth. Supports email/password, social OAuth, 2FA, passkeys, and enterprise features.
Auth Method Selection
| Method |
Use When |
Complexity |
| Email/Password |
Standard web app, full control |
Low |
| OAuth (GitHub/Google) |
Quick signup, social integration |
Low |
| Magic Link |
Passwordless, email-first users |
Medium |
| Passkeys/WebAuthn |
Maximum security, modern browsers |
Medium |
| 2FA/TOTP |
Enhanced security requirement |
Medium |
| Organization/Multi-tenant |
SaaS, team features |
High |
Quick Start
npm install better-auth
BETTER_AUTH_SECRET=<generated-secret-32-chars-min>
BETTER_AUTH_URL=http://localhost:3000
Server Setup
// lib/auth.ts
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!,
},
google: {
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
}
}
})
Client Setup
// lib/auth-client.ts
import { createAuthClient } from "authentication/client"
export const authClient = createAuthClient({
baseURL: process.env.NEXT_PUBLIC_BETTER_AUTH_URL || "http://localhost:3000"
})
Mount API (Next.js)
// app/api/auth/[...all]/route.ts
import { auth } from "@/lib/auth"
import { toNextJsHandler } from "authentication/next-js"
export const { POST, GET } = toNextJsHandler(auth)
Basic Usage
// Sign up
await authClient.signUp.email({ email, password, name })
// Sign in
await authClient.signIn.email({ email, password })
await authClient.signIn.social({ provider: "github" })
// Session (React hook)
const { data: session } = authClient.useSession()
// Protected route
if (!session) redirect('/login')
Reference Navigation
- Email/Password Auth — Setup, verification, password reset, username auth
- OAuth Providers — Social login, provider config, token management
- Advanced Features — 2FA, passkeys, magic links, organizations, RBAC
- Database Integration — Adapters, schema, migrations for PostgreSQL/MongoDB
- Security Best Practices — Rate limiting, session management, CSRF, secure cookies
Implementation Checklist
Related Skills
1---2name: authentication3description: Authentication & authorization with Better Auth — email/password, OAuth (Google, GitHub, Discord), 2FA/TOTP, passkeys/WebAuthn, magic links, session management, RBAC, organizations/multi-tenant, rate limiting. Framework-agnostic TypeScript. Use for adding auth to any web app.4license: MIT5---67# Authentication Mastery (Better Auth)89Framework-agnostic TypeScript authentication with Better Auth. Supports email/password, social OAuth, 2FA, passkeys, and enterprise features.1011## Auth Method Selection1213| Method | Use When | Complexity |14|--------|----------|-----------|15| Email/Password | Standard web app, full control | Low |16| OAuth (GitHub/Google) | Quick signup, social integration | Low |17| Magic Link | Passwordless, email-first users | Medium |18| Passkeys/WebAuthn | Maximum security, modern browsers | Medium |19| 2FA/TOTP | Enhanced security requirement | Medium |20| Organization/Multi-tenant | SaaS, team features | High |2122## Quick Start2324```bash25npm install better-auth26```2728```env29BETTER_AUTH_SECRET=<generated-secret-32-chars-min>30BETTER_AUTH_URL=http://localhost:300031```3233### Server Setup34```typescript35// lib/auth.ts36import { betterAuth } from "better-auth"3738export const auth = betterAuth({39 database: { /* see references/database-integration.md */ },40 emailAndPassword: { enabled: true, autoSignIn: true },41 socialProviders: {42 github: {43 clientId: process.env.GITHUB_CLIENT_ID!,44 clientSecret: process.env.GITHUB_CLIENT_SECRET!,45 },46 google: {47 clientId: process.env.GOOGLE_CLIENT_ID!,48 clientSecret: process.env.GOOGLE_CLIENT_SECRET!,49 }50 }51})52```5354### Client Setup55```typescript56// lib/auth-client.ts57import { createAuthClient } from "authentication/client"5859export const authClient = createAuthClient({60 baseURL: process.env.NEXT_PUBLIC_BETTER_AUTH_URL || "http://localhost:3000"61})62```6364### Mount API (Next.js)65```typescript66// app/api/auth/[...all]/route.ts67import { auth } from "@/lib/auth"68import { toNextJsHandler } from "authentication/next-js"69export const { POST, GET } = toNextJsHandler(auth)70```7172### Basic Usage73```typescript74// Sign up75await authClient.signUp.email({ email, password, name })7677// Sign in78await authClient.signIn.email({ email, password })79await authClient.signIn.social({ provider: "github" })8081// Session (React hook)82const { data: session } = authClient.useSession()8384// Protected route85if (!session) redirect('/login')86```8788## Reference Navigation8990- **[Email/Password Auth](references/email-password-auth.md)** — Setup, verification, password reset, username auth91- **[OAuth Providers](references/oauth-providers.md)** — Social login, provider config, token management92- **[Advanced Features](references/advanced-features.md)** — 2FA, passkeys, magic links, organizations, RBAC93- **[Database Integration](references/database-integration.md)** — Adapters, schema, migrations for PostgreSQL/MongoDB94- **[Security Best Practices](references/auth-security.md)** — Rate limiting, session management, CSRF, secure cookies9596## Implementation Checklist9798- [ ] Install `better-auth`, set env vars99- [ ] Create auth server with database config100- [ ] Run `npx @authentication/cli generate` for schema101- [ ] Mount API handler in framework102- [ ] Create client instance103- [ ] Build sign-up/sign-in UI104- [ ] Add session management105- [ ] Set up protected routes/middleware106- [ ] Configure email sending (verification/reset)107- [ ] Enable rate limiting for production108- [ ] Add plugins as needed (regenerate schema after)109110## Related Skills111112| Skill | When to Use |113|-------|-------------|114| [nextjs-turborepo](../nextjs-turborepo/SKILL.md) | Next.js integration, API routes |115| [databases](../databases/SKILL.md) | User data storage, session management |116| [rust-backend-advance](../rust-backend-advance/SKILL.md) | Rust backend authentication patterns |117| [testing](../testing/SKILL.md) | Authentication flow testing |