Honestly, I just wanted a nice rainbow gradient banner at the top.
Fullstack Authentication Snippets/Cloudflare better auth

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.json

Prerequisites

Cloudflare Account

  1. Install Wrangler CLI:
npm install -g wrangler
  1. Authenticate:
wrangler login

PostgreSQL Database

Get a connection string from any provider:

Format: postgresql://user:password@host:port/database

Configuration

1. Environment Variables

Create .dev.vars:

.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:3000

2. Database Setup

Create Hyperdrive

wrangler hyperdrive create my-hyperdrive \
  --connection-string="your-postgres-connection-string"

Update wrangler.toml

wrangler.toml
[[hyperdrive]]
binding = "HYPERDRIVE"
id = "your-hyperdrive-id"

Generate Schema

npx better-auth generate
npx drizzle-kit push

Development

npm run dev
# API available at http://localhost:8787

Test 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 deploy

API Endpoints

  • POST /api/auth/sign-up
  • POST /api/auth/sign-in/email
  • POST /api/auth/sign-out
  • POST /api/auth/forgot-password
  • POST /api/auth/reset-password
  • POST /api/auth/verify-email
  • GET /api/auth/session

Resources

How is this guide?

Last updated: 6/2/2025