This project is an authentication system boilerplate using Auth.js (NextAuth) on Cloudflare Workers.
- Next.js 15 (OpenNextJS 1.0.4)
- NextAuth 5.0.0-beta
- React 19
- Tailwind CSS 3
- Cloudflare D1 (Serverless Relational Database)
- Cloudflare Turnstile (Bot Protection)
- Lemon Squeezy (Payment Processing)
- i18n (Internationalization)
- Wrangler
- User authentication storage using Cloudflare D1 database
- Email authentication (magic link) using Resend
- Google OAuth authentication
- Cloudflare Turnstile bot protection integration
- Lemon Squeezy payment processing and subscription management
- Multi-language support with i18n (English, Korean, Japanese, Spanish, Chinese)
- Cloudflare deployment optimization with Open NextJS
- Responsive dashboard UI with shadcn components
- TypeScript support
- Built-in authentication middleware and session provider
- Interactive charts and data tables in dashboard
- Modern UI components with Tailwind CSS
git clone https://github.com/cwjohnpark/cloudflare-authjs-sample.git
cd cloudflare-authjs-samplenpm install- Create an account and log in to the Cloudflare Dashboard.
- Create a new project in Workers & Pages.
- Create a D1 database.
Copy the .env.example file to create your local environment configuration:
cp .env.example .env.localThen edit the .env.local file with your actual values:
AUTH_SECRET="randomly generated long string"
AUTH_RESEND_KEY="API key from Resend"
AUTH_EMAIL_FROM="no-reply@yourdomain.com"
AUTH_URL="http://localhost:8787/"
AUTH_GOOGLE_ID="Google OAuth Client ID"
AUTH_GOOGLE_SECRET="Google OAuth Client Secret"
NEXT_PUBLIC_CLOUDFLARE_TURNSTILE_SITE_KEY="Cloudflare Turnstile Site Key"
CLOUDFLARE_TURNSTILE_SECRET_KEY="Cloudflare Turnstile Secret Key"
LEMON_SQUEEZY_API_KEY="Lemon Squeezy API Key"
LEMON_SQUEEZY_WEBHOOK_SIGNING_KEY="Lemon Squeezy Webhook Secret"Environment Variables Explanation:
AUTH_SECRET: Can be generated using the commandnpx auth secret.AUTH_RESEND_KEY: Get an API key by creating an account on Resend.AUTH_EMAIL_FROM: Sender email address for authentication emails.AUTH_URL: Local URL for development, deployed domain URL for production.AUTH_GOOGLE_ID/SECRET: OAuth credentials from respective consoles. AuthJS Google ProviderNEXT_PUBLIC_CLOUDFLARE_TURNSTILE_SITE_KEY: Public site key for Cloudflare Turnstile bot protection. Cloudflare TurnstileCLOUDFLARE_TURNSTILE_SECRET_KEY: Secret key for Cloudflare Turnstile server-side verification.LEMON_SQUEEZY_API_KEY: API key for Lemon Squeezy payment processing. Lemon Squeezy APILEMON_SQUEEZY_WEBHOOK_SIGNING_KEY: Webhook secret for Lemon Squeezy payment events. Note: For Cloudflare Workers deployment, you'll also need to configure these same variables in your Cloudflare dashboard or use.dev.varsfor local development with Wrangler.
Edit the following items in your wrangler.jsonc file:
For local development with Next.js development server:
npm run devFor testing with Cloudflare Workers runtime (preview mode):
npm run previewDeploy to Cloudflare Workers:
npm run deploy- Google OAuth: One-click login with Google account
- Email (Resend): Magic link authentication via email
- Cloudflare Turnstile: Bot protection for authentication forms
- Custom session provider for global session state
- Server-side session validation
- Automatic redirect based on authentication status
- Supported Languages: English, Korean, Japanese, Spanish, Chinese
- Dynamic Language Switching: Real-time language switching without page reload
- URL-based Localization: Language-specific URLs (e.g.,
/en/dashboard,/ko/dashboard) - Automatic Language Detection: Browser language preference detection
- Localized Content: All UI elements, messages, and content are fully localized
- Subscription Management: Handle subscription creation, updates, and cancellations
- Payment Processing: Secure payment processing with webhook support
- Billing Dashboard: User-friendly billing interface with subscription details
- Webhook Handling: Real-time payment event processing
Call GET /api/setup to automatically create and migrate D1 database schema using Auth.js D1 adapter.
The dashboard includes:
- Interactive Charts: Area charts with multiple data series
- Data Tables: Advanced tables with sorting, filtering, and pagination
- Responsive Sidebar: Collapsible navigation with user profile
- Cards Section: Overview cards with key metrics
- Billing Management: Subscription and payment management interface
- Language Switcher: Multi-language support with language selection dropdown
- Modern UI: Built with shadcn/ui components
During development, you can only send testing emails to your own email address. To send emails to other recipients in production, verify a domain at resend.com/domains and change the from address to use the verified domain.
Calling GET /api/setup produces the local D1 database at .wrangler/state/v3/d1/miniflare-D1DatabaseObject.
The project uses Next.js App Router internationalization with the following structure:
- Language dictionaries are located in
src/lib/i18n/dictionaries/ - Supported languages:
en,ko,ja,es,zh - URL structure:
/[lang]/[route](e.g.,/en/dashboard,/ko/settings) - Language switcher component available in the navigation
- Fork this repository
- Create a new branch:
git checkout -b feature/your-feature-name - Commit your changes:
git commit -m 'Add some feature' - Push to your forked repository:
git push origin feature/your-feature-name - Submit a Pull Request
This project is distributed under the MIT License.
This project was inspired by and built upon the official Cloudflare tutorial, recreated with modern implementations and additional features.
{ "name": "your-project-name", "d1_databases": [ { "binding": "DB", "database_name": "your-database-name", "database_id": "your-database-id" } ] }