Hono + Cloudflare Workers Integration
Authentication API with Better Auth on Cloudflare's edge network
Installation
npx shadcn@latest add https://snippets.thedevdavid.com/r/hono-cloudflare-better-auth-server.jsonPrerequisites
PostgreSQL Database
Get a connection string from any provider:
Format: postgresql://user:password@host:port/database
Configuration
1. Environment Variables
Create .dev.vars:
BETTER_AUTH_SECRET=your-secret-key-min-32-chars
BETTER_AUTH_URL=http://localhost:8787
EMAIL_FROM_ADDRESS=noreply@yourdomain.com
CORS_ORIGINS=http://localhost:30002. Database Setup
Create Hyperdrive
wrangler hyperdrive create my-hyperdrive \
--connection-string="your-postgres-connection-string"Update wrangler.toml
[[hyperdrive]]
binding = "HYPERDRIVE"
id = "your-hyperdrive-id"Generate Schema
npx better-auth generate
npx drizzle-kit pushDevelopment
npm run dev
# API available at http://localhost:8787Test Authentication
# Sign up
curl -X POST http://localhost:8787/api/auth/sign-up \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com", "password": "password123"}'
# Sign in
curl -X POST http://localhost:8787/api/auth/sign-in/email \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com", "password": "password123"}'Deploy
# Set secrets
wrangler secret put BETTER_AUTH_SECRET
# Deploy
npm run deployAPI Endpoints
POST /api/auth/sign-upPOST /api/auth/sign-in/emailPOST /api/auth/sign-outPOST /api/auth/forgot-passwordPOST /api/auth/reset-passwordPOST /api/auth/verify-emailGET /api/auth/session
Resources
How is this guide?
Last updated: 6/2/2025