A proof-of-concept authentication system built with Cloudflare Workers and Durable Objects. This project demonstrates a scalable, distributed authentication system using Cloudflare's edge computing platform.
The system uses Durable Objects (DOs) for user data storage, with a clever partitioning strategy:
- Each user has two DOs:
- Email-based DO: Used for login lookups
- ID-based DO: Used for authenticated session data
This dual-DO approach provides:
- Fast email-based lookups for login
- Secure ID-based access for authenticated routes
- Natural horizontal scaling
- No central database or lookup table
- 🔐 Secure password hashing using WebCrypto API
- 🍪 HTTP-only cookie-based JWT authentication
- 📧 Email-based user lookup
- 🔄 Automatic session management
- 🚀 Edge-based authentication
- 📈 Horizontally scalable architecture
- Cloudflare Workers
- Durable Objects
- Hono (Web Framework)
- JWT for session tokens
- WebCrypto API for password hashing
- PBKDF2 password hashing with 100k iterations
- HTTP-only cookies
- Secure cookie settings
- JWT-based session management
- No password storage in cookies
- Clone the repository
- Install dependencies:
npm install
- Set up your environment variables:
JWT_SECRET=your-secret-here
- Run locally:
npm run dev
GET /signup
- Signup pagePOST /signup
- Create new accountGET /login
- Login pagePOST /login
- Authenticate userGET /
- Protected profile pagePOST /logout
- End session
-
Signup:
- User submits email/password
- System creates two DOs (email-based and ID-based)
- Returns JWT token in HTTP-only cookie
-
Login:
- User submits email/password
- System looks up user by email DO
- Verifies password hash
- Returns JWT token in HTTP-only cookie
-
Protected Routes:
- System verifies JWT token
- Looks up user data by ID DO
- Returns protected content
Durable Objects provide:
- Strong consistency
- Low latency
- Automatic scaling
- No database management
- Built-in state management
This makes them perfect for:
- User session management
- Authentication state
- Distributed user data
- Edge-based authentication