$npx -y skills add jackspace/ClaudeSkillz --skill better-auth_mrgoonieImplement authentication and authorization with Better Auth - a framework-agnostic TypeScript authentication framework. Features include email/password authentication with verification, OAuth providers (Google, GitHub, Discord, etc.), two-factor authentication (TOTP, SMS), passke
| 1 | # Better Auth Skill |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | ## When to Use |
| 6 | |
| 7 | - Implementing auth in TypeScript/JavaScript applications |
| 8 | - Adding email/password or social OAuth authentication |
| 9 | - Setting up 2FA, passkeys, magic links, advanced auth features |
| 10 | - Building multi-tenant apps with organization support |
| 11 | - Managing sessions and user lifecycle |
| 12 | - Working with any framework (Next.js, Nuxt, SvelteKit, Remix, Astro, Hono, Express, etc.) |
| 13 | |
| 14 | ## Quick Start |
| 15 | |
| 16 | ### Installation |
| 17 | |
| 18 | ```bash |
| 19 | npm install better-auth |
| 20 | # or pnpm/yarn/bun add better-auth |
| 21 | ``` |
| 22 | |
| 23 | ### Environment Setup |
| 24 | |
| 25 | Create `.env`: |
| 26 | ```env |
| 27 | BETTER_AUTH_SECRET=<generated-secret-32-chars-min> |
| 28 | BETTER_AUTH_URL=http://localhost:3000 |
| 29 | ``` |
| 30 | |
| 31 | ### Basic Server Setup |
| 32 | |
| 33 | Create `auth.ts` (root, lib/, utils/, or under src/app/server/): |
| 34 | |
| 35 | ```ts |
| 36 | import { betterAuth } from "better-auth"; |
| 37 | |
| 38 | export const auth = betterAuth({ |
| 39 | database: { |
| 40 | // See references/database-integration.md |
| 41 | }, |
| 42 | emailAndPassword: { |
| 43 | enabled: true, |
| 44 | autoSignIn: true |
| 45 | }, |
| 46 | socialProviders: { |
| 47 | github: { |
| 48 | clientId: process.env.GITHUB_CLIENT_ID!, |
| 49 | clientSecret: process.env.GITHUB_CLIENT_SECRET!, |
| 50 | } |
| 51 | } |
| 52 | }); |
| 53 | ``` |
| 54 | |
| 55 | ### Database Schema |
| 56 | |
| 57 | ```bash |
| 58 | npx @better-auth/cli generate # Generate schema/migrations |
| 59 | npx @better-auth/cli migrate # Apply migrations (Kysely only) |
| 60 | ``` |
| 61 | |
| 62 | ### Mount API Handler |
| 63 | |
| 64 | **Next.js App Router:** |
| 65 | ```ts |
| 66 | // app/api/auth/[...all]/route.ts |
| 67 | import { auth } from "@/lib/auth"; |
| 68 | import { toNextJsHandler } from "better-auth/next-js"; |
| 69 | |
| 70 | export const { POST, GET } = toNextJsHandler(auth); |
| 71 | ``` |
| 72 | |
| 73 | **Other frameworks:** See references/email-password-auth.md#framework-setup |
| 74 | |
| 75 | ### Client Setup |
| 76 | |
| 77 | Create `auth-client.ts`: |
| 78 | |
| 79 | ```ts |
| 80 | import { createAuthClient } from "better-auth/client"; |
| 81 | |
| 82 | export const authClient = createAuthClient({ |
| 83 | baseURL: process.env.NEXT_PUBLIC_BETTER_AUTH_URL || "http://localhost:3000" |
| 84 | }); |
| 85 | ``` |
| 86 | |
| 87 | ### Basic Usage |
| 88 | |
| 89 | ```ts |
| 90 | // Sign up |
| 91 | await authClient.signUp.email({ |
| 92 | email: "user@example.com", |
| 93 | password: "SecureP@ssw0rd!2024", // Use strong passwords with uppercase, numbers, and symbols |
| 94 | name: "John Doe" |
| 95 | }); |
| 96 | |
| 97 | // Sign in |
| 98 | await authClient.signIn.email({ |
| 99 | email: "user@example.com", |
| 100 | password: "SecureP@ssw0rd!2024" |
| 101 | }); |
| 102 | |
| 103 | // OAuth |
| 104 | await authClient.signIn.social({ provider: "github" }); |
| 105 | |
| 106 | // Session |
| 107 | const { data: session } = authClient.useSession(); // React/Vue/Svelte |
| 108 | const { data: session } = await authClient.getSession(); // Vanilla JS |
| 109 | ``` |
| 110 | |
| 111 | ## Feature Selection Matrix |
| 112 | |
| 113 | | Feature | Plugin Required | Use Case | Reference | |
| 114 | |---------|----------------|----------|-----------| |
| 115 | | Email/Password | No (built-in) | Basic auth | [email-password-auth.md](./references/email-password-auth.md) | |
| 116 | | OAuth (GitHub, Google, etc.) | No (built-in) | Social login | [oauth-providers.md](./references/oauth-providers.md) | |
| 117 | | Email Verification | No (built-in) | Verify email addresses | [email-password-auth.md](./references/email-password-auth.md#email-verification) | |
| 118 | | Password Reset | No (built-in) | Forgot password flow | [email-password-auth.md](./references/email-password-auth.md#password-reset) | |
| 119 | | Two-Factor Auth (2FA/TOTP) | Yes (`twoFactor`) | Enhanced security | [advanced-features.md](./references/advanced-features.md#two-factor-authentication) | |
| 120 | | Passkeys/WebAuthn | Yes (`passkey`) | Passwordless auth | [advanced-features.md](./references/advanced-features.md#passkeys-webauthn) | |
| 121 | | Magic Link | Yes (`magicLink`) | Email-based login | [advanced-features.md](./references/advanced-features.md#magic-link) | |
| 122 | | Username Auth | Yes (`username`) | Username login | [email-password-auth.md](./references/email-password-auth.md#username-authentication) | |
| 123 | | Organizations/Multi-tenant | Yes (`organization`) | Team/org features | [advanced-features.md](./references/advanced-features.md#organizations) | |
| 124 | | Rate Limiting | No (built-in) | Prevent abuse | [advanced-features.md](./references/advanced-features.md#rate-limiting) | |
| 125 | | Session Management | No (built-in) | User sessions | [advanced-features.md](./references/advanced-features.md#session-management) | |
| 126 | |
| 127 | ## Auth Method Selection Guide |
| 128 | |
| 129 | **Choose Email/Password when:** |
| 130 | - Building standard web app with traditional auth |
| 131 | - Need full control over |