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-auth3description: "TypeScript authentication framework (framework-agnostic). Features: email/password, OAuth (Google, GitHub, Discord), 2FA (TOTP, SMS), passkeys/WebAuthn, session management, RBAC, rate limiting, database adapters. Actions: implement, configure, secure authentication systems. Keywords: Better Auth, authentication, authorization, OAuth, email/password, 2FA, MFA, TOTP, passkeys, WebAuthn, session management, RBAC, rate limiting, database adapter, TypeScript auth, social login, Google auth, GitHub auth, Discord auth, email verification, password reset. Use when: implementing TypeScript auth, adding OAuth providers, setting up 2FA/MFA, managing sessions, configuring RBAC, building secure auth systems."4license: MIT5version: 2.0.06metadata:7 source: https://skillmd.com/skills/aibot88/better-auth8---910# Better Auth Skill1112Better Auth is comprehensive, framework-agnostic authentication/authorization framework for TypeScript with built-in email/password, social OAuth, and powerful plugin ecosystem for advanced features.1314## When to Use1516- Implementing auth in TypeScript/JavaScript applications17- Adding email/password or social OAuth authentication18- Setting up 2FA, passkeys, magic links, advanced auth features19- Building multi-tenant apps with organization support20- Managing sessions and user lifecycle21- Working with any framework (Next.js, Nuxt, SvelteKit, Remix, Astro, Hono, Express, etc.)2223## Quick Start2425### Installation2627```bash28npm install better-auth29# or pnpm/yarn/bun add better-auth30```3132### Environment Setup3334Create `.env`:35```env36BETTER_AUTH_SECRET=<generated-secret-32-chars-min>37BETTER_AUTH_URL=http://localhost:300038```3940### Basic Server Setup4142Create `auth.ts` (root, lib/, utils/, or under src/app/server/):4344```ts45import { betterAuth } from "better-auth";4647export const auth = betterAuth({48 database: {49 // See references/database-integration.md50 },51 emailAndPassword: {52 enabled: true,53 autoSignIn: true54 },55 socialProviders: {56 github: {57 clientId: process.env.GITHUB_CLIENT_ID!,58 clientSecret: process.env.GITHUB_CLIENT_SECRET!,59 }60 }61});62```6364### Database Schema6566```bash67npx @better-auth/cli generate # Generate schema/migrations68npx @better-auth/cli migrate # Apply migrations (Kysely only)69```7071### Mount API Handler7273**Next.js App Router:**74```ts75// app/api/auth/[...all]/route.ts76import { auth } from "@/lib/auth";77import { toNextJsHandler } from "better-auth/next-js";7879export const { POST, GET } = toNextJsHandler(auth);80```8182**Other frameworks:** See references/email-password-auth.md#framework-setup8384### Client Setup8586Create `auth-client.ts`:8788```ts89import { createAuthClient } from "better-auth/client";9091export const authClient = createAuthClient({92 baseURL: process.env.NEXT_PUBLIC_BETTER_AUTH_URL || "http://localhost:3000"93});94```9596### Basic Usage9798```ts99// Sign up100await authClient.signUp.email({101 email: "user@example.com",102 password: "secure123",103 name: "John Doe"104});105106// Sign in107await authClient.signIn.email({108 email: "user@example.com",109 password: "secure123"110});111112// OAuth113await authClient.signIn.social({ provider: "github" });114115// Session116const { data: session } = authClient.useSession(); // React/Vue/Svelte117const { data: session } = await authClient.getSession(); // Vanilla JS118```119120## Feature Selection Matrix121122| Feature | Plugin Required | Use Case | Reference |123|---------|----------------|----------|-----------|124| Email/Password | No (built-in) | Basic auth | [email-password-auth.md](./references/email-password-auth.md) |125| OAuth (GitHub, Google, etc.) | No (built-in) | Social login | [oauth-providers.md](./references/oauth-providers.md) |126| Email Verification | No (built-in) | Verify email addresses | [email-password-auth.md](./references/email-password-auth.md#email-verification) |127| Password Reset | No (built-in) | Forgot password flow | [email-password-auth.md](./references/email-password-auth.md#password-reset) |128| Two-Factor Auth (2FA/TOTP) | Yes (`twoFactor`) | Enhanced security | [advanced-features.md](./references/advanced-features.md#two-factor-authentication) |129| Passkeys/WebAuthn | Yes (`passkey`) | Passwordless auth | [advanced-features.md](./references/advanced-features.md#passkeys-webauthn) |130| Magic Link | Yes (`magicLink`) | Email-based login | [advanced-features.md](./references/advanced-features.md#magic-link) |131| Username Auth | Yes (`username`) | Username login | [email-password-auth.md](./references/email-password-auth.md#username-authentication) |132| Organizations/Multi-tenant | Yes (`organization`) | Team/org features | [advanced-features.md](./references/advanced-features.md#organizations) |133| Rate Limiting | No (built-in) | Prevent abuse | [advanced-features.md](./references/advanced-features.md#rate-limiting) |134| Session Management | No (built-in) | User sessions | [advanced-features.md](./references/advanced-features.md#session-management) |135136## Auth Method Selection Guide137138**Choose Email/Password when:**139- Building standard web app with traditional auth140- Need full control over user credentials141- Targeting users who prefer email-based accounts142143**Choose OAuth when:**144- Want quick signup with minimal friction145- Users already have social accounts146- Need access to social profile data147148**Choose Passkeys when:**149- Want passwordless experience150- Targeting modern browsers/devices151- Security is top priority152153**Choose Magic Link when:**154- Want passwordless without WebAuthn complexity155- Targeting email-first users156- Need temporary access links157158**Combine Multiple Methods when:**159- Want flexibility for different user preferences160- Building enterprise apps with various auth requirements161- Need progressive enhancement (start simple, add more options)162163## Core Architecture164165Better Auth uses client-server architecture:1661. **Server** (`better-auth`): Handles auth logic, database ops, API routes1672. **Client** (`better-auth/client`): Provides hooks/methods for frontend1683. **Plugins**: Extend both server/client functionality169170## Implementation Checklist171172- [ ] Install `better-auth` package173- [ ] Set environment variables (SECRET, URL)174- [ ] Create auth server instance with database config175- [ ] Run schema migration (`npx @better-auth/cli generate`)176- [ ] Mount API handler in framework177- [ ] Create client instance178- [ ] Implement sign-up/sign-in UI179- [ ] Add session management to components180- [ ] Set up protected routes/middleware181- [ ] Add plugins as needed (regenerate schema after)182- [ ] Test complete auth flow183- [ ] Configure email sending (verification/reset)184- [ ] Enable rate limiting for production185- [ ] Set up error handling186187## Reference Documentation188189### Core Authentication190- [Email/Password Authentication](./references/email-password-auth.md) - Email/password setup, verification, password reset, username auth191- [OAuth Providers](./references/oauth-providers.md) - Social login setup, provider configuration, token management192- [Database Integration](./references/database-integration.md) - Database adapters, schema setup, migrations193194### Advanced Features195- [Advanced Features](./references/advanced-features.md) - 2FA/MFA, passkeys, magic links, organizations, rate limiting, session management196197## Scripts198199- `scripts/better_auth_init.py` - Initialize Better Auth configuration with interactive setup200201## Resources202203- Docs: https://www.better-auth.com/docs204- GitHub: https://github.com/better-auth/better-auth205- Plugins: https://www.better-auth.com/docs/plugins206- Examples: https://www.better-auth.com/docs/examples