Skip to content

AbbasRostami/NextAuth-v5-OAuth-Authentication

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

73 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ” NextAuth v5 OAuth Authentication

๐Ÿ”ง Tech Stack โš™๏ธ

Next.js ย ย  TypeScript ย ย  Tailwind CSS ย ย  NextAuth.js ย ย  Prisma ORM ย ย  JWT ย ย  MongoDB Atlas ย ย  bcryptjs

This is a Next.js 15 authentication project using NextAuth v5 with OAuth providers (GitHub, Google, etc.) and MongoDB Atlas for database management.

  • ๐ŸŒŸ Features

  • โœ… OAuth Providers: Google, GitHub & More

  • ๐Ÿ”’ Protected Routes with Middleware

  • ๐ŸŽจ Tailwind CSS Styled

  • ๐Ÿ›ก๏ธ Session Encryption & CSRF Protection

  • ๐Ÿ“ฆ Prisma ORM with MongoDB Atlas

๐Ÿ› ๏ธ Tech Stack

  • Next.js 15 (App Router)
  • NextAuth.js v5
  • MongoDB Atlas (Database)
  • Prisma ORM
  • Tailwind CSS (Styling)

๐Ÿ—‚๏ธ Project Architecture

Project Structure

๐Ÿ“ฆ nextauth-v5-oauth-auth
โ”œโ”€โ”€ ๐Ÿ“œ Core Foundation
โ”‚   โ”œโ”€โ”€ next.config.ts       # โš™๏ธ Next.js Advanced Config
โ”‚   โ”œโ”€โ”€ tailwind.config.ts   # ๐ŸŽจ Design System Setup
โ”‚   โ””โ”€โ”€ tsconfig.json        # ๐Ÿ› ๏ธ TypeScript Rules
โ”‚
โ”œโ”€โ”€ ๐Ÿ” Auth Infrastructure
โ”‚   โ”œโ”€โ”€ prisma/schema.prisma # ๐Ÿ—ƒ๏ธ Database Schema
โ”‚   โ”œโ”€โ”€ src/auth.ts          # ๐Ÿ”‘ Auth Configuration
โ”‚   โ””โ”€โ”€ src/middleware.ts    # ๐Ÿ›ก๏ธ Security Layer
โ”‚
โ”œโ”€โ”€ โšก๏ธ Action Controllers
โ”‚   โ””โ”€โ”€ src/actions/
โ”‚       โ”œโ”€โ”€ formActions.ts   # ๐Ÿ“ Form Handling
โ”‚       โ””โ”€โ”€ postActions.ts   # ๐Ÿ“ฎ Content Management
โ”‚
โ”œโ”€โ”€ ๐ŸŽจ UI Components
โ”‚   โ”œโ”€โ”€ public/              # ๐Ÿ“ Static Assets
โ”‚   โ””โ”€โ”€ src/app/
โ”‚       โ”œโ”€โ”€ components/      # ๐Ÿงฉ Atomic Design System
โ”‚       โ””โ”€โ”€ globals.css      # ๐ŸŒˆ Global Styles
โ”‚
โ”œโ”€โ”€ ๐ŸŒ API Gateway
โ”‚   โ””โ”€โ”€ src/app/api/
โ”‚       โ””โ”€โ”€ auth/
โ”‚           โ”œโ”€โ”€ [...nextauth] # ๐Ÿ”— NextAuth Core
โ”‚           โ”œโ”€โ”€ login/        # ๐Ÿ–‹๏ธ Custom Auth Flow
โ”‚           โ””โ”€โ”€ signup/       # ๐Ÿ“ Registration Handler
โ”‚
โ””โ”€โ”€ ๐Ÿ› ๏ธ Development Toolkit
    โ”œโ”€โ”€ .env.example         # ๐Ÿ“‹ Configuration Template
    โ”œโ”€โ”€ eslint.config.mjs    # โœจ Code Quality
    โ””โ”€โ”€ postcss.config.mjs   # ๐ŸŽจ CSS Processing

๐Ÿ› ๏ธ Installation & Setup

1๏ธโƒฃ Clone the repository

git clone https://github.com/AbbasRostami/nextauth-v5-oauth-authentication.git
cd nextauth-v5-oauth-authentication

2๏ธโƒฃ Install dependencies

yarn install  # or npm install

3๏ธโƒฃ Set up environment variables

Create a .env.local file and copy the values from .env.example, then update them with your credentials.

# Create environment file
cp .env.example .env.local
๐Ÿ›ก๏ธ NextAuth Configuration
NEXTAUTH_SECRET="your_ultra_secure_secret_key"  # Generate using: openssl rand -base64 32
NEXTAUTH_URL=http://localhost:3000             # Development URL
NEXTAUTH_TRUST_HOST=true                       # Enable in development

# ๐Ÿ”‘ Authentication Providers
# ============================

# ๐Ÿ™ GitHub OAuth
GITHUB_CLIENT_ID="your_github_oauth_client_id"
GITHUB_CLIENT_SECRET="your_github_oauth_client_secret"

# ๐Ÿ“ง Google OAuth
AUTH_GOOGLE_ID="your_google_oauth_client_id.apps.googleusercontent.com"
AUTH_GOOGLE_SECRET="your_google_oauth_client_secret"

# ๐Ÿ—„๏ธ Database Configuration
# =========================
DATABASE_URL="mongodb+srv://<username>:<password>@cluster.mongodb.net/<dbname>?retryWrites=true&w=majority"

# ๐Ÿ”’ Security Tokens
# ==================
JWT_SECRET="your_jwt_encryption_key"  # Generate using: openssl rand -base64 32

# ๐ŸŒ API Configuration
# ====================
NEXT_PUBLIC_API_URL="http://localhost:3000"

4๏ธโƒฃ Run the project

yarn dev  # or npm run dev

Application will be running at: http://localhost:3000

sequenceDiagram
    participant User
    participant Client
    participant Server
    participant NextAuth
    participant Prisma
    participant Middleware

    User->>Client: Submit Credentials
    Client->>Server: POST /api/auth/[action]
    activate Server
    
    Server->>NextAuth: Process Request
    activate NextAuth
    NextAuth->>Prisma: Query Database
    activate Prisma
    Prisma-->>NextAuth: User Data
    deactivate Prisma
    
    alt Valid
        NextAuth->>Client: Set HttpOnly Cookie
    else Invalid
        NextAuth->>Client: Error Response
    end
    deactivate NextAuth
    deactivate Server

    Client->>Middleware: Request Route
    activate Middleware
    Middleware->>NextAuth: Verify Session
    activate NextAuth
    NextAuth-->>Middleware: Session Status
    deactivate NextAuth
    
    alt Valid
        Middleware->>Server: Forward Request
        Server->>Client: Protected Content
    else Invalid
        Middleware->>Client: Redirect
    end
    deactivate Middleware
Loading

๐Ÿ—๏ธ Core Architecture

๐Ÿ”„ Authentication Flow

๐Ÿ“ก API Endpoints

Endpoint Method Description
/api/auth/login POST User login with JWT issuance
/api/auth/signup POST New user registration

๐Ÿ”‘ Authentication Providers

This project uses NextAuth v5 for authentication. You can add more OAuth providers by modifying src/auth.ts.

๐Ÿ” Protected Routes

Some routes (/posts, /users) are protected and require authentication. Middleware in middleware.ts handles access control.

๐Ÿ“œ License

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


Happy Coding! ๐Ÿš€
Developed with โค๏ธ by Abbas Rostami

GitHub Stars

Releases

No releases published

Packages

No packages published