nextjs15-postgres-nextauth-tailwindcss-template Tailwind Templates

Nextjs15 Postgres Nextauth Tailwindcss Template

Updated for NextJS 15

Next.js 15 + NextAuth v5 Starter Kit ๐Ÿš€

A production-ready, feature-complete authentication starter kit with modern best practices.


โœจ Features

๐Ÿ” Authentication ๐Ÿ› ๏ธ Development ๐Ÿงช Testing
  • NextAuth v5 Integration
  • Email/Password Authentication
  • Secure Session Management
  • Protected Routes
  • Sign In/Sign Out Flow
  • Next.js 15 App Router
  • TypeScript for Type Safety
  • Server Actions
  • Drizzle ORM with PostgreSQL
  • Tailwind CSS
  • Playwright E2E Tests
  • Page Object Model Pattern
  • Auth Flow Testing
  • Visual Testing UI
  • Test Coverage

๐Ÿ“š Table of Contents

๐Ÿš€ Quick Start

Prerequisites

  • Node.js (18.17.0 or later)
  • PostgreSQL database

Step 1: Clone & Install

# Clone the repository
git clone https://github.com/your-username/next15-nextauth-boilerplate.git

# Navigate to the project
cd next15-nextauth-boilerplate

# Install dependencies
npm install

Step 2: Environment Setup

Create a .env.local file in the root directory:

# Required environment variables
AUTH_SECRET=your-auth-secret
POSTGRES_URL=your-postgres-url

Generate a secure AUTH_SECRET:

openssl rand -base64 32

Step 3: Database Setup

# Generate database migrations
npm run db:generate

# Run migrations
npm run db:migrate

Step 4: Start Development Server

npm run dev

Visit http://localhost:3000 to see your application running.

๐Ÿ“‚ Project Structure

next15-nextauth-boilerplate/
โ”‚
โ”œโ”€โ”€ ๐Ÿ—‚๏ธ app/                      # Next.js App Router
โ”‚   โ”œโ”€โ”€ (auth)/                 # Authentication routes
โ”‚   โ”‚   โ”œโ”€โ”€ auth.config.ts      # NextAuth configuration
โ”‚   โ”‚   โ”œโ”€โ”€ auth.ts             # Auth setup and providers
โ”‚   โ”‚   โ”œโ”€โ”€ actions.ts          # Server actions (login/register)
โ”‚   โ”‚   โ”œโ”€โ”€ login/              # Login page
โ”‚   โ”‚   โ””โ”€โ”€ register/           # Registration page
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ (dashboard)/            # Protected routes
โ”‚       โ”œโ”€โ”€ layout.tsx          # Dashboard layout
โ”‚       โ””โ”€โ”€ page.tsx            # Dashboard home
โ”‚
โ”œโ”€โ”€ ๐Ÿงฉ components/               # Reusable React components
โ”‚   โ”œโ”€โ”€ auth-form.tsx           # Authentication form
โ”‚   โ”œโ”€โ”€ sign-out-form.tsx       # Sign out component
โ”‚   โ””โ”€โ”€ ui/                     # UI components
โ”‚
โ”œโ”€โ”€ ๐Ÿ“Š lib/                      # Utilities and libraries
โ”‚   โ””โ”€โ”€ db/                     # Database utilities
โ”‚       โ”œโ”€โ”€ schema.ts           # Drizzle schema definition
โ”‚       โ”œโ”€โ”€ queries.ts          # Database operations
โ”‚       โ””โ”€โ”€ migrations/         # Generated migrations
โ”‚
โ”œโ”€โ”€ ๐Ÿงช tests/                    # Test files
โ”‚   โ”œโ”€โ”€ auth.setup.ts           # Authentication test setup
โ”‚   โ”œโ”€โ”€ auth.test.ts            # Auth flow tests
โ”‚   โ””โ”€โ”€ dashboard.test.ts       # Dashboard tests
โ”‚
โ”œโ”€โ”€ ๐Ÿ”ง middleware.ts             # NextAuth middleware
โ”œโ”€โ”€ โš™๏ธ drizzle.config.ts         # Drizzle ORM configuration
โ”œโ”€โ”€ ๐ŸŽญ playwright.config.ts      # Playwright test configuration
โ””โ”€โ”€ ๐Ÿ“ README.md                 # You are here!

๐Ÿ” Authentication System

This starter kit implements a complete authentication system using NextAuth v5 with email/password authentication.

๐Ÿ“‹ Authentication Flow

Authentication flow diagram

1๏ธโƒฃ Registration Process

sequenceDiagram
    User->>+Frontend: Fill registration form
    Frontend->>+Server: Submit credentials
    Server->>Server: Validate input with Zod
    Server->>Server: Hash password with bcrypt
    Server->>+Database: Check if user exists
    Database-->>-Server: User exists/doesn't exist
    alt User exists
        Server-->>Frontend: Return error
    else User doesn't exist
        Server->>Database: Create new user
        Server->>Server: Create session
        Server-->>Frontend: Success & redirect
    end
    Frontend-->>-User: Show success/error

2๏ธโƒฃ Login Process

  • User submits credentials via login form
  • Server validates the input
  • Server checks credentials against database
  • If valid, a session is created and user is redirected
  • If invalid, an error message is displayed

3๏ธโƒฃ Protected Routes

The middleware protects routes by:

  • Checking for valid session
  • Redirecting unauthenticated users to login
  • Redirecting authenticated users away from auth pages

4๏ธโƒฃ Sign Out

  • User clicks "Sign out" button
  • Session is destroyed
  • User is redirected to login page

๐Ÿ› ๏ธ Key Authentication Files

File Purpose
auth.config.ts Core NextAuth configuration
auth.ts NextAuth setup with providers
actions.ts Server actions for auth operations
middleware.ts Route protection

๐Ÿ’พ Database Setup

This starter uses Drizzle ORM with PostgreSQL for type-safe database access.

๐Ÿ“Š Database Schema

// lib/db/schema.ts
export const user = pgTable('User', {
  id: uuid('id').primaryKey().notNull().defaultRandom(),
  email: varchar('email', { length: 64 }).notNull(),
  password: varchar('password', { length: 64 }),
});

๐Ÿ” Database Operations

The starter includes two main database operations:

  1. Creating users - Securely hashes passwords before storage:

    export async function createUser(email: string, password: string)
    
  2. Retrieving users - Finds users by email for authentication:

    export async function getUser(email: string): Promise<Array<User>>
    

๐Ÿ”„ Migrations

Database migrations are handled automatically:

# Generate migrations after schema changes
npm run db:generate

# Apply migrations to your database
npm run db:migrate

๐Ÿงช Running Tests

This starter includes comprehensive end-to-end tests with Playwright.

๐Ÿงฉ Test Structure

Tests are organized using the Page Object Model pattern for better maintainability:

// Example of AuthPage class
class AuthPage {
  constructor(private page: Page) {}
  
  async login(email: string, password: string) {
    await this.gotoLogin();
    await this.page.getByLabel('Email Address').fill(email);
    await this.page.getByLabel('Password').fill(password);
    await this.page.getByRole('button', { name: 'Sign In' }).click();
  }
  
  // Other authentication methods...
}

๐Ÿšฆ Running the Test Suite

# Install Playwright browsers (first time only)
npx playwright install

# Run all tests
npm run test

# Run tests with visual UI
npm run test:ui

๐Ÿ“‹ What's Tested

Feature Tests
Registration โœ… New user registration
โœ… Existing email validation
โœ… Password requirements
Login โœ… Valid credentials
โœ… Invalid credentials
โœ… Form validation
Protected Routes โœ… Authenticated access
โœ… Unauthenticated redirects
Dashboard โœ… User info display
โœ… Sign out functionality

๐Ÿš€ Deployment

Deploy with Vercel
  1. Push your code to GitHub, GitLab, or Bitbucket
  2. Import the project on Vercel
  3. Configure your environment variables:
    • AUTH_SECRET
    • POSTGRES_URL
  4. Deploy!

Alternative Hosting

For other hosting providers, ensure you:

  1. Build the application with npm run build
  2. Set up the required environment variables
  3. Configure your database connection
  4. Deploy the built application

Made with โค๏ธ for Next.js developers

MIT License

Top categories

Loading Svelte Themes