$npx -y skills add jackspace/ClaudeSkillz --skill better-authProduction-ready authentication framework for TypeScript with first-class Cloudflare D1 support. Use this skill when building auth systems as a self-hosted alternative to Clerk or Auth.js, particularly for Cloudflare Workers projects. Supports social providers (Google, GitHub, Mi
| 1 | # better-auth Skill |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | **better-auth** is a comprehensive, framework-agnostic authentication and authorization library for TypeScript. It provides a complete auth solution with first-class support for Cloudflare D1, making it an excellent self-hosted alternative to Clerk or Auth.js. |
| 6 | |
| 7 | **Use this skill when**: |
| 8 | - Building authentication for Cloudflare Workers + D1 applications |
| 9 | - Need a self-hosted, vendor-independent auth solution |
| 10 | - Migrating from Clerk (avoid vendor lock-in) |
| 11 | - Upgrading from Auth.js (need more features) |
| 12 | - Implementing multi-tenant SaaS with organizations/teams |
| 13 | - Require advanced features: 2FA, passkeys, RBAC, social auth |
| 14 | |
| 15 | **Package**: `better-auth@1.3.34` (latest verified 2025-10-31) |
| 16 | |
| 17 | --- |
| 18 | |
| 19 | ## Installation |
| 20 | |
| 21 | ### Core Package |
| 22 | |
| 23 | ```bash |
| 24 | npm install better-auth |
| 25 | # or |
| 26 | pnpm add better-auth |
| 27 | # or |
| 28 | yarn add better-auth |
| 29 | ``` |
| 30 | |
| 31 | ### Database Adapters |
| 32 | |
| 33 | **For Cloudflare D1** (Workers): |
| 34 | ```bash |
| 35 | npm install @cloudflare/workers-types |
| 36 | ``` |
| 37 | |
| 38 | **For PostgreSQL**: |
| 39 | ```bash |
| 40 | npm install pg drizzle-orm |
| 41 | ``` |
| 42 | |
| 43 | **For MySQL/SQLite**: Built-in adapters, no extra packages needed. |
| 44 | |
| 45 | ### Social Providers (Optional) |
| 46 | |
| 47 | ```bash |
| 48 | npm install @better-auth/google |
| 49 | npm install @better-auth/github |
| 50 | npm install @better-auth/microsoft |
| 51 | ``` |
| 52 | |
| 53 | --- |
| 54 | |
| 55 | ## Quick Start Patterns |
| 56 | |
| 57 | ### Pattern 1: Cloudflare Workers + D1 |
| 58 | |
| 59 | **Use when**: Building API on Cloudflare Workers with D1 database |
| 60 | |
| 61 | **File**: `src/worker.ts` |
| 62 | ```typescript |
| 63 | import { betterAuth } from 'better-auth' |
| 64 | import { d1Adapter } from 'better-auth/adapters/d1' |
| 65 | import { Hono } from 'hono' |
| 66 | |
| 67 | type Env = { |
| 68 | DB: D1Database |
| 69 | BETTER_AUTH_SECRET: string |
| 70 | GOOGLE_CLIENT_ID: string |
| 71 | GOOGLE_CLIENT_SECRET: string |
| 72 | } |
| 73 | |
| 74 | const app = new Hono<{ Bindings: Env }>() |
| 75 | |
| 76 | // Auth routes handler |
| 77 | app.all('/api/auth/*', async (c) => { |
| 78 | const auth = betterAuth({ |
| 79 | database: d1Adapter(c.env.DB), |
| 80 | secret: c.env.BETTER_AUTH_SECRET, |
| 81 | |
| 82 | // Basic auth methods |
| 83 | emailAndPassword: { |
| 84 | enabled: true, |
| 85 | requireEmailVerification: true |
| 86 | }, |
| 87 | |
| 88 | // Social providers |
| 89 | socialProviders: { |
| 90 | google: { |
| 91 | clientId: c.env.GOOGLE_CLIENT_ID, |
| 92 | clientSecret: c.env.GOOGLE_CLIENT_SECRET |
| 93 | } |
| 94 | } |
| 95 | }) |
| 96 | |
| 97 | return auth.handler(c.req.raw) |
| 98 | }) |
| 99 | |
| 100 | export default app |
| 101 | ``` |
| 102 | |
| 103 | **wrangler.toml**: |
| 104 | ```toml |
| 105 | name = "my-app" |
| 106 | main = "src/worker.ts" |
| 107 | compatibility_date = "2024-01-01" |
| 108 | |
| 109 | [[d1_databases]] |
| 110 | binding = "DB" |
| 111 | database_name = "my-app-db" |
| 112 | database_id = "your-database-id" |
| 113 | |
| 114 | [vars] |
| 115 | # Public vars here |
| 116 | |
| 117 | # Secrets (use: wrangler secret put BETTER_AUTH_SECRET) |
| 118 | # - BETTER_AUTH_SECRET |
| 119 | # - GOOGLE_CLIENT_ID |
| 120 | # - GOOGLE_CLIENT_SECRET |
| 121 | ``` |
| 122 | |
| 123 | **Setup D1 Database**: |
| 124 | ```bash |
| 125 | # Create database |
| 126 | wrangler d1 create my-app-db |
| 127 | |
| 128 | # Generate migration SQL from better-auth |
| 129 | npx better-auth migrate --database d1 |
| 130 | |
| 131 | # Apply migration |
| 132 | wrangler d1 execute my-app-db --remote --file migrations/0001_initial.sql |
| 133 | ``` |
| 134 | |
| 135 | --- |
| 136 | |
| 137 | ### Pattern 2: Next.js API Route |
| 138 | |
| 139 | **Use when**: Building traditional Next.js app with PostgreSQL or D1 |
| 140 | |
| 141 | **File**: `src/lib/auth.ts` |
| 142 | ```typescript |
| 143 | import { betterAuth } from 'better-auth' |
| 144 | import { Pool } from 'pg' |
| 145 | |
| 146 | export const auth = betterAuth({ |
| 147 | database: new Pool({ |
| 148 | connectionString: process.env.DATABASE_URL |
| 149 | }), |
| 150 | |
| 151 | secret: process.env.BETTER_AUTH_SECRET!, |
| 152 | |
| 153 | emailAndPassword: { |
| 154 | enabled: true, |
| 155 | requireEmailVerification: true, |
| 156 | sendVerificati |