Skip to content

Latest commit

Β 

History

History
85 lines (68 loc) Β· 3.13 KB

File metadata and controls

85 lines (68 loc) Β· 3.13 KB

Next.js 16 + Better Auth + Drizzle Template

A modern, production-ready Next.js 16 starter template featuring robust authentication, database management, and optimized route organization.

πŸš€ Features

  • Next.js 16 (App Router): Utilizing the latest React 19 features and Turbopack.
  • Better Auth: Comprehensive authentication system with session management.
  • Automated Migrations: Database migrations are automatically applied on application launch.
  • Drizzle ORM: Type-safe database interactions with PostgreSQL.
  • Route Groups & Security Proxy:
    • (authenticated): Private routes protected by a server-side layout proxy.
    • (guest): Public-only routes (Login/Register) with auto-redirect for logged-in users.
  • Path Aliases: Standardized @/* imports for clean and reliable module resolution.
  • Biome: Ultra-fast linting and formatting.
  • Tailwind CSS 4: Modern styling with native CSS variable support.

πŸ“ Project Structure

β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ (authenticated)/  # Protected routes (requires login)
β”‚   β”‚   β”œβ”€β”€ layout.tsx    # Security Proxy & Permission check
β”‚   β”‚   └── page.tsx      # Dashboard / Home
β”‚   β”œβ”€β”€ (guest)/          # Public routes (login, register)
β”‚   β”‚   β”œβ”€β”€ layout.tsx    # Guest-only redirect logic
β”‚   β”‚   β”œβ”€β”€ login/
β”‚   β”‚   └── register/
β”‚   β”œβ”€β”€ api/auth/         # Better Auth API handlers
β”‚   └── layout.tsx        # Root layout
β”œβ”€β”€ components/           # UI Components (shadcn/ui)
β”œβ”€β”€ lib/
β”‚   β”œβ”€β”€ db/               # Drizzle schema and client
β”‚   β”œβ”€β”€ auth.ts           # Server-side Auth config
β”‚   └── auth-client.ts    # Client-side Auth hook
└── drizzle/              # Generated migrations

πŸ› οΈ Getting Started

1. Environment Setup

Copy .env.example to .env.local and fill in your credentials:

cp .env.example .env.local

2. Database

Start the local database using Docker:

bun db:up

Generate migrations after making changes to your schema:

bun db:generate

Note: Migrations are automatically applied when the application starts (via instrumentation.ts). You can also run them manually using bun db:migrate.

3. Development

Install dependencies and run the server:

bun install
bun dev

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

πŸ“œ Available Scripts

  • bun dev: Start development server with Turbopack.
  • bun lint:fix: Run Biome to fix linting and formatting.
  • bun db:generate: Generate migration files from your schema.
  • bun db:migrate: Manually apply migrations to the database.
  • bun db:studio: Open Drizzle Studio to explore your data.

πŸ”’ Security & Permissions

The app/(authenticated)/layout.tsx file acts as a centralized security proxy. It checks for a valid session before rendering any children.

A commented-out placeholder is available in the layout to implement role-based access control (RBAC):

// const userRole = session.user.role;
// if (!allowedRoles.includes(userRole)) redirect('/dashboard?error=denied');