This is a Cloudflare Workers-based user management framework that provides serverless authentication and session management. It consists of three main components:
- user-mgmt: Handles user registration, login, password reset, and authentication using D1 database
- session-state: Manages session data using KV storage with distributed edge persistence
- account-pages: Example frontend Pages application demonstrating authentication flows
- Authentication: SHA-256 password hashing (optimized for Workers constraints)
- Session Management: Service-to-service communication between workers
- Database: D1 for user data persistence, KV for session storage
- API Design: RESTful endpoints with CORS support for cross-origin requests
- RBAC System: Optional role-based access control with hierarchical permissions
- Local development:
lerna run dev(in root) ornpm run dev(in package directory) - Worker ports: session-state: 51511, user-mgmt: 51512
- Pages dev server: http://localhost:48080
- Inspector ports: session-state: 51521, user-mgmt: 51522, account-pages: 9230
- Deploy all:
lerna run deploy(deploys all workers and pages) - Deploy single:
npm run deploy(from specific package directory) - Database migrations:
npx wrangler d1 execute users --file=./schema.sql --remote
- Linting: Run configured linters before committing
- Type checking: TypeScript strict mode enabled
- Local API: User management API runs on http://localhost:51512 in dev mode
- Formatting: 4-space indentation, opening braces on same line
- Naming: camelCase for variables/functions, PascalCase for interfaces/types/classes
- Constants: UPPER_SNAKE_CASE, defined at top of files
- TypeScript: Explicit parameter/return types, minimize use of
any - Imports: Group by source, locals first then externals
- Error handling: Try/catch with proper context, appropriate HTTP status codes
- Async: Always use async/await pattern for asynchronous operations
- Documentation: JSDoc-style comments for functions and interfaces
- Security: Never log or expose secrets, use environment variables for sensitive data
workers-users/
├── packages/
│ ├── user-mgmt/ # User authentication worker
│ │ ├── src/ # TypeScript source files
│ │ ├── migrations/ # D1 database migrations
│ │ └── wrangler.toml # Worker configuration
│ ├── session-state/ # Session management worker
│ │ ├── src/ # TypeScript source files
│ │ └── wrangler.toml # Worker configuration
│ └── account-pages/ # Example frontend application
│ ├── static/ # HTML, CSS, JS files
│ └── wrangler.toml # Pages configuration
├── lerna.json # Lerna monorepo configuration
└── package.json # Root package with workspaces
- itty-router: Lightweight router for Workers (v5.0.17)
- @cloudflare/workers-types: TypeScript types for Workers APIs
- wrangler: Cloudflare CLI for development and deployment
EMAIL_FROM: Sender email for password resetEMAIL_FROM_NAME: Display name for emailsEMAIL_DKIM_SELECTOR: DKIM selector for email authenticationEMAIL_DKIM_DOMAIN: Domain for DKIM signingFORGOT_PASSWORD_URL: Reset password page URLTOKEN_VALID_MINUTES: Password reset token validity (default: 60)RBAC_ENABLED: Enable role-based access control (default: false)RBAC_DEFAULT_ROLE: Default role for new users (optional)SUPER_ADMIN_EMAIL: Email for initial admin user (optional)SUPER_ADMIN_EMAIL_CONFIRMED: Must be "true" to confirm admin email ownership before bootstrap (security feature)LOG_IP_ADDRESS: Enable IP address logging in audit logs (GDPR consideration - default: false)
- D1 Database:
usersDB- User data storage - KV Namespace:
sessionstore- Session data storage - Service Binding:
sessionService- Connection to session-state worker
POST /register- User registrationPOST /login- User authenticationPOST /logout- Session terminationPOST /forgot-password- Initiate password resetPOST /forgot-password-validate- Validate reset tokenPOST /forgot-password-new-password- Update passwordGET /load-user- Get current user data
GET /api/roles- List all rolesGET /api/roles/:roleId- Get role detailsPOST /api/roles- Create new rolePUT /api/roles/:roleId- Update roleDELETE /api/roles/:roleId- Delete roleGET /api/users/:userId/roles- Get user's rolesPOST /api/users/:userId/roles- Assign role to userDELETE /api/users/:userId/roles/:roleId- Remove role from userGET /api/users/:userId/permissions- Get user's effective permissionsPOST /api/permissions/check- Check specific permissions
POST /create- Create new sessionGET /get/:sessionId- Retrieve session dataPUT /update/:sessionId- Update session dataPATCH /add/:sessionId- Add data to sessionDELETE /delete/:sessionId- Delete session
- Email functionality uses MailChannels (free tier for Workers)
- CORS is configured for cross-origin requests with credentials
- Session cookies should be on same domain as API for Safari compatibility
- Use route-based deployment for same-domain API hosting (e.g., /user-api/*)