foxlau/react-router-better-auth
Tentang
π This is a template that can be deployed on Cloudflare Workers, built with React Router v8+ (Remix), Better Auth, Drizzle ORM, and D1.
Pengembangan web dan aplikasiYang dapat Anda lakukan
- π§ Email and Password Authentication - Secure login with email and password
- π Password Recovery - Forgot password and reset password functionality
- π Social Login - Sign in with Google and GitHub accounts
- π€ Session Management - Secure session handling with Cloudflare D1 storage
Pertumbuhan bintang terukur
- dalam 24 jam
- 0
- dalam 7 hari
- 0
- dalam 15 hari
- +1
- dalam 1 bulan
- Mengumpulkan riwayat
Perubahan bersih pada periode yang tumpang tindih, bukan total harian. Pengukuran yang belum tersedia dibiarkan kosong. Bintang menunjukkan perhatian, bukan kualitas.
README.md
AsliReact Routers with Better auth.
This template features React Router, Better auth, Drizzle ORM, and D1, designed for deployment on Cloudflare Workers.
π Authentication Features
This template implements a complete authentication system using Better Auth with the following features:
- π§ Email and Password Authentication - Secure login with email and password
- π Password Recovery - Forgot password and reset password functionality
- π Social Login - Sign in with Google and GitHub accounts
- π€ Session Management - Secure session handling with Cloudflare D1 storage
- ποΈ Account Management - Including account deletion functionality
- π Admin Panel - Complete admin dashboard with user management, role-based access control
β Core Features
- π Server-side rendering
- β‘οΈ Hot Module Replacement (HMR)
- π¦ Asset bundling and optimization
- π Data loading and mutations
- π TypeScript by default
- π TailwindCSS and Shadcn for UI styling
- π Better Auth for authentication
- π§οΈ Drizzle ORM for database
- π’οΈ Cloudflare D1 for database
- π React Router docs
Demo
Here's a preview of the app:
For more demo images, check the .assets directory.
Links
React Router v7 Authentication Demo Series:
- React Router v7 Cloudflare workers template - React Router v7 Cloudflare workers template.
- React Router v7 with Remix Auth - Multi-strategy authentication demo using Remix Auth
- React Router v7 with Vite+ - React Router 7 + Vite+ full-stack template β Hono SSR, Drizzle/SQLite, Better Auth, Paraglide i18n.
Getting Started
Installation
Use Bun 1.4.2+ and Node.js 24 (Node.js 22.22+ is the minimum supported by React Router's tooling), then install the dependencies:
git clone https://github.com/foxlau/react-router-better-auth.git
bun install
Bun is the package manager for this project. Commit bun.lock when dependencies change. The Shadcn CLI is not a runtime dependency; use bunx shadcn@latest add <component> when scaffolding UI components.
CI runs bun audit before type checks, tests, and the production build. The dependency overrides select patched Browserslist, fast-uri, js-yaml, and Undici releases, and replace the vulnerable esbuild version in Drizzle Kit's legacy loader without changing Wrangler's esbuild. Review and remove these overrides when the upstream dependency chains are patched.
Development
Run an initial database migration:
cp .env.example .env
cp wrangler.jsonc.example wrangler.jsonc
bun run db:migrate:local
bun run db:seed:local
If you modify the Drizzle ORM schema, please run bun run db:generate first. If you need to delete the generated SQL migrations, execute bun run db:drop and select the SQL migration you wish to remove.
Better Auth uses its built-in D1 database storage for rate limiting (rate_limits), sessions (sessions), and verification records (verifications). No KV namespace is required. After pulling schema updates, run bun run db:migrate:local before starting development, and bun run db:migrate:remote before deploying to production. Database joins are enabled with advanced.database.joins.
When upgrading from KV-backed authentication, existing sessions and pending verification/reset flows are not migrated. Users must sign in again or request a new verification/reset link.
Server-side authorization reads fresh sessions rather than trusting the cookie cache. The admin layout checks the configured dashboard: read permission in middleware before child loaders/actions; both admin and editor roles can access the dashboard.
Start the development server with HMR:
bun run dev
Your application will be available at http://localhost:5173.
Default Users (from seed)
After running bun run db:seed:local, you can use the following default user to sign in:
- Admin User:
[email protected]/admin@8899
Troubleshooting
If you encounter errors after pulling updates from GitHub, try cleaning the cache and dependencies:
# Clean build cache and restart (preserve .wrangler local database data)
rm -rf .react-router node_modules/.vite
bun run dev
# If the above doesn't work, reinstall dependencies using the existing lockfile
rm -rf node_modules
bun install --frozen-lockfile
bun run dev
Database Commands
bun run db:generate- Generate new database migration filesbun run db:migrate:local- Apply migrations to local databasebun run db:migrate:remote- Apply migrations to production databasebun run db:drop- Remove generated SQL migrations (interactive)bun run db:seed:local- Seed local database with initial data fromdrizzle/seed/seed.sql(safe to repeat)bun run db:seed:remote- Seed production database with initial data fromdrizzle/seed/seed.sqlbun run db:reset:local- Delete all local application/auth data while preserving tables and migration historybun run db:delete:local- Delete local database files
Authentication Commands
bun run auth:secret- Generate a new Better Auth secret key. Use this when setting up authentication for the first time or when rotating secrets. The generated secret should be added to your.envfile asBETTER_AUTH_SECRET.bun run auth:generate- Generate and format the Drizzle/SQLite schema fromapp/services/auth/auth.server.tsatapp/db/schema/auth.ts, without connecting to D1. It uses the configured adapter so plural table names are preserved. Review changes before generating/applying migrations.
These commands use the local auth CLI, pinned to match the installed Better Auth version. The old @better-auth/cli package is deprecated; update the CLI alongside Better Auth.
Other Useful Commands
bun run typecheck- Run TypeScript type checkingbun run test- Run Vitest regression tests in the local Workers runtime with isolated D1/R2 storagebun run test:watch- Run Vitest in watch modebun run check- Check formatting, lint rules, and TypeScript typesbun run check:fix- Format code and apply safe lint fixesbun run format- Format code with Oxfmtbun run format:check- Check formatting without changing filesbun run lint- Lint code with Oxlintbun run lint:fix- Apply safe Oxlint fixesbun run preview- Preview production build locally
Building for Production
Create a production build:
bun run build
Deployment
Deployment is done using the Wrangler CLI.
Create the D1 database and R2 bucket, then update d1_databases and r2_buckets in wrangler.jsonc with the generated resource details:
bunx wrangler d1 create rr7-better-auth
bunx wrangler r2 bucket create rr7-better-auth
To deploy directly to production:
bun run db:migrate:remote
bun run deploy
To deploy a preview URL:
bun run deploy:version
You can then promote a version to production after verification or roll it out progressively.
bun run deploy:promote
Dependency Updates and Auto-Merge
Dependabot checks Bun dependencies (package.json and bun.lock) and GitHub Actions every Monday at 09:00 Asia/Shanghai. Minor and patch updates are grouped; major updates remain separate for manual review.
.github/workflows/dependabot-auto-merge.yml enables squash auto-merge only for verified Dependabot minor/patch PRs without maintainer changes. It never checks out or executes PR code with its write-capable token. Other PRs are not auto-merged.
Repository setup:
- Enable Settings β General β Pull Requests β Allow auto-merge and squash merging.
- Protect
mainwith the required GitHub Actions checks π§Ή Oxc lint and π Type check, and require branches to be up to date. The type-check job also runs regression tests and the production build. - Keep any required reviews enabled; auto-merge waits for them and does not automatically approve PRs. No personal access token is required.
- For public repositories, require workflow approval for all outside collaborators, keep default workflow permissions read-only, and leave Allow GitHub Actions to create and approve pull requests disabled. Only the isolated Dependabot metadata workflow requests scoped write permissions; PR validation does not retain checkout credentials.
The automation only enables auto-merge for Dependabot. Maintainers with write access may still manually enable GitHub's native auto-merge on other PRs, subject to the same branch protection rules.
Questions
If you have any questions, please open an issue.
Jelajahi proyek terkait
Memuatβ¦