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
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
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. Use when this capability is needed.4---56# Authentication Mastery (Better Auth)78Framework-agnostic TypeScript authentication with Better Auth. Supports email/password, social OAuth, 2FA, passkeys, and enterprise features.910## Auth Method Selection1112| Method | Use When | Complexity |13|--------|----------|-----------|14| Email/Password | Standard web app, full control | Low |15| OAuth (GitHub/Google) | Quick signup, social integration | Low |16| Magic Link | Passwordless, email-first users | Medium |17| Passkeys/WebAuthn | Maximum security, modern browsers | Medium |18| 2FA/TOTP | Enhanced security requirement | Medium |19| Organization/Multi-tenant | SaaS, team features | High |2021## Quick Start2223```bash24npm install better-auth25```2627```env28BETTER_AUTH_SECRET=<generated-secret-32-chars-min>29BETTER_AUTH_URL=http://localhost:300030```3132### Server Setup33```typescript34// lib/auth.ts35import { betterAuth } from "better-auth"3637export const auth = betterAuth({38 database: { /* see references/database-integration.md */ },39 emailAndPassword: { enabled: true, autoSignIn: true },40 socialProviders: {41 github: {42 clientId: process.env.GITHUB_CLIENT_ID!,43 clientSecret: process.env.GITHUB_CLIENT_SECRET!,44 },45 google: {46 clientId: process.env.GOOGLE_CLIENT_ID!,47 clientSecret: process.env.GOOGLE_CLIENT_SECRET!,48 }49 }50})51```5253### Client Setup54```typescript55// lib/auth-client.ts56import { createAuthClient } from "authentication/client"5758export const authClient = createAuthClient({59 baseURL: process.env.NEXT_PUBLIC_BETTER_AUTH_URL || "http://localhost:3000"60})61```6263### Mount API (Next.js)64```typescript65// app/api/auth/[...all]/route.ts66import { auth } from "@/lib/auth"67import { toNextJsHandler } from "authentication/next-js"68export const { POST, GET } = toNextJsHandler(auth)69```7071### Basic Usage72```typescript73// Sign up74await authClient.signUp.email({ email, password, name })7576// Sign in77await authClient.signIn.email({ email, password })78await authClient.signIn.social({ provider: "github" })7980// Session (React hook)81const { data: session } = authClient.useSession()8283// Protected route84if (!session) redirect('/login')85```8687## Reference Navigation8889- **[Email/Password Auth](references/email-password-auth.md)** — Setup, verification, password reset, username auth90- **[OAuth Providers](references/oauth-providers.md)** — Social login, provider config, token management91- **[Advanced Features](references/advanced-features.md)** — 2FA, passkeys, magic links, organizations, RBAC92- **[Database Integration](references/database-integration.md)** — Adapters, schema, migrations for PostgreSQL/MongoDB93- **[Security Best Practices](references/auth-security.md)** — Rate limiting, session management, CSRF, secure cookies9495## Implementation Checklist9697- [ ] Install `better-auth`, set env vars98- [ ] Create auth server with database config99- [ ] Run `npx @authentication/cli generate` for schema100- [ ] Mount API handler in framework101- [ ] Create client instance102- [ ] Build sign-up/sign-in UI103- [ ] Add session management104- [ ] Set up protected routes/middleware105- [ ] Configure email sending (verification/reset)106- [ ] Enable rate limiting for production107- [ ] Add plugins as needed (regenerate schema after)108109## Related Skills110111| Skill | When to Use |112|-------|-------------|113| [nextjs-turborepo](../nextjs-turborepo/SKILL.md) | Next.js integration, API routes |114| [databases](../databases/SKILL.md) | User data storage, session management |115| [rust-backend-advance](../rust-backend-advance/SKILL.md) | Rust backend authentication patterns |116| [testing](../testing/SKILL.md) | Authentication flow testing |117118---119> Converted and distributed by [TomeVault](https://tomevault.io/claim/thienty1207) — claim your Tome and manage your conversions.120<!-- tomevault:4.0:skill_md:2026-04-14 -->