Add authentication with Better Auth (TypeScript). Use for email/password, OAuth providers, 2FA/MFA, passkeys/WebAuthn, sessions, RBAC, rate limiting.
Comprehensive, framework-agnostic authentication for TypeScript with email/password, social OAuth, and a powerful plugin ecosystem.
npm install better-auth
BETTER_AUTH_SECRET=<32-char-min-secret>
BETTER_AUTH_URL=http://localhost:3000
// 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!,
}
}
});
npx @better-auth/cli generate # Generate schema/migrations
npx @better-auth/cli migrate # Apply (Kysely only)
// 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);
import { createAuthClient } from "better-auth/client";
export const authClient = createAuthClient({
baseURL: process.env.NEXT_PUBLIC_BETTER_AUTH_URL
});
| Feature | Plugin Required | Reference |
|---|---|---|
| Email/Password | No | references/email-password-auth.md |
| OAuth (GitHub, Google, etc.) | No | references/oauth-providers.md |
| Email Verification | No | references/email-password-auth.md |
| 2FA/TOTP | Yes (twoFactor) | references/advanced-features.md |
| Passkeys/WebAuthn | Yes (passkey) | references/advanced-features.md |
| Magic Link | Yes (magicLink) | references/advanced-features.md |
| Organizations/Multi-tenant | Yes (organization) | references/advanced-features.md |
| Rate Limiting | No (built-in) | references/advanced-features.md |
references/email-password-auth.md — Email/password, verification, resetreferences/oauth-providers.md — Social login setupreferences/database-integration.md — DB adapters, schema, migrationsreferences/advanced-features.md — 2FA, passkeys, organizations, sessions