This is a monorepo for a starter project utilizing Cloudflare D1 as a database with a robust authentication system. The project is split into a server (Cloudflare Workers) and a client (React with Vite). It provides a complete setup for managing users, tokens, and a database with Drizzle ORM.
pnpm
workspaces for seamless development.The repository uses a monorepo structure with pnpm
workspaces:
client/
: React-based frontend using Vite and Tailwind CSS.server/
: Backend built on Cloudflare Workers with Hono and Drizzle ORM.The root package includes global scripts:
dev
: Starts both client and server in development mode.build
: Builds both client and server for production.start
: Runs the production server.lint
: Lints all packages using ESLint.Ensure you have pnpm
installed. If not, you can install it globally:
npm install -g pnpm
Once installed, run the following command in the project root to install all dependencies for both client and server:
pnpm install
Both the server and client require .env
files for configuration. Create the following .env
files:
.env
D1_DATABASE=d1_database_name
CLOUDFLARE_D1_TOKEN=your_d1_token
CLOUDFLARE_DATABASE_ID=your_database_id
CLOUDFLARE_ACCOUNT_ID=your_account_id
NODE_ENV=development
.env
VITE_APP_NAME="App"
VITE_APP_API_BASE_URL=http://localhost:8787
You can also create .env.development
and .env.production
files for environment-specific configurations.
In wrangler.toml
, update the following variables:
account_id = "YOUR-ACCOUNT-ID"
[[d1_databases]]
binding = "AUTH_DB"
database_name = "d1-auth"
database_id = "YOUR-DATABASE-ID"
migrations_dir = "drizzle/migrations"
[[kv_namespaces]]
binding = "SESSION_KV"
id = "YOUR-KV-ID"
preview_id = "YOUR-KV-ID"
[vars]
NODE_ENV = "production"
Make sure the values align with your .env
file.
To automate builds on Cloudflare, add the following secrets to your GitHub repository:
CF_API_TOKEN
: Your Cloudflare API Token.CF_ACCOUNT_ID
: Your Cloudflare Account ID.CF_PROJECT_ID
: Your Cloudflare Pages project ID (if using Pages).VITE_APP_NAME
: Your app's name.VITE_APP_API_BASE_URL
: API URL.The server is built with Cloudflare Workers and uses the following stack:
dev
: Starts Wrangler for development and Drizzle Studio.generate:migration
: Generates a new migration file.push:migration
: Pushes migrations to the local D1 database.Run the following to create a local database and apply schema:
wrangler d1 execute d1-auth --file=schema.sql
To create the database schema in the remote D1 environment, run:
wrangler d1 execute d1-auth --file=schema.sql --remote
Run the following command to start both the client and server in development mode:
pnpm dev
To build the client and server:
pnpm build
To deploy the project to Cloudflare Workers:
pnpm deploy
As an independent developer, your support means the world to me. Every piece of feedback, download, and kind word helps me continue creating unique projects like this one. Thank you for being a part of this journey and making it possible to bring my ideas to life.