alexasomba/nextjs-latest-starter-kit-template

โ˜… 0Forks 0TypeScriptGitHub โ†—Compare

README

Next.js Latest Starter Kit Template

Deploy to Cloudflare Pages MIT License CI Status

A production-ready Next.js 16 starter template optimized for Cloudflare Pages deployment with better-auth-cloudflare integration.

Features

  • โšก Next.js 16 with React 19 and Turbopack
  • ๐Ÿ” Authentication via better-auth-cloudflare
    • Anonymous login support
    • Geolocation tracking
    • OpenAPI documentation generation
  • ๐Ÿ—„๏ธ Database - Drizzle ORM with Cloudflare D1
  • ๐Ÿ“ฆ File Storage - Cloudflare R2 bucket integration
  • ๐Ÿ”‘ Key-Value Store - Cloudflare KV namespace support
  • ๐ŸŽจ UI Components - Tailwind CSS 4 + shadcn/ui components
  • ๐Ÿ”ง Code Quality - Biome for formatting and linting
  • ๐Ÿš€ Cloudflare Workers - Native Cloudflare platform integration

Technology Stack

  • Framework: Next.js 16 (App Router)
  • Runtime: React 19
  • Styling: Tailwind CSS 4, shadcn/ui
  • Authentication: better-auth + better-auth-cloudflare
  • Database: Drizzle ORM + Cloudflare D1
  • Storage: Cloudflare R2 (object storage)
  • Cache: Cloudflare KV (key-value store)
  • Deployment: Cloudflare Pages via @opennextjs/cloudflare
  • Code Quality: Biome

Prerequisites

Getting Started

1. Clone and Install

git clone <your-repo-url>
cd nextjs-latest-starter-kit-template
npm install
# or
bun install

2. Set Up Cloudflare Resources

You'll need to create the following Cloudflare resources:

Create a D1 Database

wrangler d1 create DATABASE

Copy the database ID and update your wrangler.jsonc:

{
  "d1_databases": [
    {
      "binding": "DATABASE",
      "database_name": "DATABASE",
      "database_id": "your-database-id"
    }
  ]
}

Create a KV Namespace

wrangler kv namespace create KV

Add the KV namespace to wrangler.jsonc:

{
  "kv_namespaces": [
    {
      "binding": "KV",
      "id": "your-kv-id"
    }
  ]
}

Create an R2 Bucket

wrangler r2 bucket create r2-bucket

Add the R2 bucket to wrangler.jsonc:

{
  "r2_buckets": [
    {
      "binding": "R2_BUCKET",
      "bucket_name": "r2-bucket"
    }
  ]
}

3. Set Up Database Schema

Generate the authentication schema:

npm run auth:update

Generate Drizzle migrations:

npm run db:generate

Apply migrations locally:

npm run db:migrate:dev

4. Run Development Server

npm run dev
# or
bun dev

Open http://localhost:3000 to see your application.

The development server uses Turbopack for fast refresh. You can start editing pages in src/app/ and see changes instantly.

Available Scripts

Development

  • npm run dev - Start development server with Turbopack
  • npm run build - Build Next.js application
  • npm run build:cf - Build for Cloudflare Pages deployment

Authentication

  • npm run auth:generate - Generate Better Auth database schema
  • npm run auth:format - Format generated schema with Prettier
  • npm run auth:update - Generate and format schema (combines above two)

Database

  • npm run db:generate - Generate Drizzle migrations
  • npm run db:migrate:dev - Apply migrations to local D1 database
  • npm run db:migrate:prod - Apply migrations to production D1 database
  • npm run db:studio:dev - Open Drizzle Studio for local database
  • npm run db:studio:prod - Open Drizzle Studio for production database

Code Quality

  • npm run format - Format code with Biome
  • npm run lint - Lint code with Biome

Deployment

  • npm run preview - Build and preview locally
  • npm run deploy - Build and deploy to Cloudflare Pages
  • npm run clean-deploy - Clean install and deploy

Utilities

  • npm run cf-typegen - Generate TypeScript types from Wrangler config
  • npm run clean - Clean build artifacts and dependencies

Project Structure

โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ app/              # Next.js app router pages
โ”‚   โ”‚   โ”œโ”€โ”€ api/          # API routes (Better Auth)
โ”‚   โ”‚   โ”œโ”€โ”€ dashboard/    # Protected dashboard page
โ”‚   โ”‚   โ””โ”€โ”€ page.tsx      # Home page with anonymous login
โ”‚   โ”œโ”€โ”€ auth/             # Authentication configuration
โ”‚   โ”‚   โ”œโ”€โ”€ index.ts      # Better Auth setup
โ”‚   โ”‚   โ””โ”€โ”€ authClient.ts # Client-side auth utilities
โ”‚   โ”œโ”€โ”€ components/       # React components
โ”‚   โ”‚   โ”œโ”€โ”€ ui/           # shadcn/ui components
โ”‚   โ”‚   โ””โ”€โ”€ FileUploadDemo.tsx
โ”‚   โ”œโ”€โ”€ db/               # Database configuration
โ”‚   โ”‚   โ”œโ”€โ”€ schema.ts     # Drizzle schema
โ”‚   โ”‚   โ””โ”€โ”€ auth.schema.ts # Generated auth schema
โ”‚   โ”œโ”€โ”€ lib/              # Utility functions
โ”‚   โ””โ”€โ”€ middleware.ts     # Next.js middleware for auth
โ”œโ”€โ”€ public/               # Static assets
โ”œโ”€โ”€ wrangler.jsonc        # Cloudflare Workers configuration
โ”œโ”€โ”€ open-next.config.ts   # OpenNext Cloudflare configuration
โ”œโ”€โ”€ next.config.ts        # Next.js configuration
โ””โ”€โ”€ biome.json            # Biome configuration

Authentication Flow

This template uses better-auth-cloudflare for authentication:

  1. Anonymous Login: Users can log in without credentials
  2. Session Management: Sessions stored in D1 database
  3. Geolocation: Automatic IP geolocation via Cloudflare
  4. Protected Routes: Middleware redirects unauthenticated users

Adding Social Providers

To add OAuth providers (GitHub, Google, etc.), update src/auth/index.ts:

socialProviders: {
  github: {
    clientId: process.env.GITHUB_CLIENT_ID || "",
    clientSecret: process.env.GITHUB_CLIENT_SECRET || "",
  },
}

Add the secrets to Cloudflare:

wrangler secret put GITHUB_CLIENT_ID
wrangler secret put GITHUB_CLIENT_SECRET

File Upload with R2

The template includes file upload functionality using Cloudflare R2:

  • Maximum file size: 2MB (configurable in src/auth/index.ts)
  • Allowed types: .jpg, .jpeg, .png, .gif (configurable)
  • Additional metadata: category, description, public flag

Files are uploaded via Better Auth's R2 plugin and can be managed through the dashboard.

Deployment

Deploy to Cloudflare Pages

  1. Apply migrations to production:
npm run db:migrate:prod
  1. Deploy:
npm run deploy

Or use the Cloudflare dashboard:

  1. Connect your GitHub repository
  2. Set build command: npm run build:cf
  3. Set build output directory: .open-next
  4. Add environment variables/bindings from your wrangler.jsonc

Environment Variables

Configure in Cloudflare Pages settings or wrangler.jsonc:

  • DATABASE - D1 database binding (required)
  • KV - KV namespace binding (required)
  • R2_BUCKET - R2 bucket binding (required)

Learn More

Next.js

Cloudflare

Better Auth

Drizzle ORM

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is open source and available under the MIT License.

Contributors

alexasombaCopilot

Issues