This is a Next.js 15 authentication project using NextAuth v5 with OAuth providers (GitHub, Google, etc.) and MongoDB Atlas for database management.
-
โ OAuth Providers: Google, GitHub & More
-
๐ Protected Routes with Middleware
-
๐จ Tailwind CSS Styled
-
๐ก๏ธ Session Encryption & CSRF Protection
-
๐ฆ Prisma ORM with MongoDB Atlas
- Next.js 15 (App Router)
- NextAuth.js v5
- MongoDB Atlas (Database)
- Prisma ORM
- Tailwind CSS (Styling)
๐ฆ 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
git clone https://github.com/AbbasRostami/nextauth-v5-oauth-authentication.git
cd nextauth-v5-oauth-authenticationyarn install # or npm install
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.localNEXTAUTH_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"
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
| Endpoint | Method | Description |
|---|---|---|
/api/auth/login |
POST |
User login with JWT issuance |
/api/auth/signup |
POST |
New user registration |
This project uses NextAuth v5 for authentication. You can add more OAuth providers by modifying src/auth.ts.
Some routes (/posts, /users) are protected and require authentication. Middleware in middleware.ts handles access control.
This project is open-source and available under the MIT License.
Happy Coding! ๐
Developed with โค๏ธ by Abbas Rostami