A comprehensive, production-ready authentication system built with Next.js 14, NextAuth.js v5, and Prisma. This project demonstrates modern authentication patterns with role-based access control, two-factor authentication, and social OAuth integration.
This project addresses the common challenge of implementing secure, scalable authentication in modern web applications. It provides:
- Secure user authentication with multiple providers
- Role-based access control (Admin/User roles)
- Two-factor authentication for enhanced security
- Email verification and password reset flows
- Multi-provider OAuth integration (Google, GitHub)
- Protected routes with middleware-based authorization
- Server and client component integration patterns
βββ app/ # Next.js 14 App Router
β βββ (protected)/ # Protected routes
β βββ auth/ # Authentication pages
β βββ api/ # API routes
βββ components/ # Reusable UI components
β βββ auth/ # Authentication-specific components
β βββ ui/ # Base UI components (shadcn/ui)
βββ data/ # Database access layer
βββ lib/ # Utility libraries
βββ actions/ # Server actions
βββ schemas/ # Zod validation schemas
βββ prisma/ # Database schema and migrations
- Next.js 14 - React framework with App Router
- NextAuth.js v5 - Authentication library
- Prisma - Database ORM with PostgreSQL
- Tailwind CSS - Styling framework
- shadcn/ui - Component library
- Zod - Schema validation
- React Hook Form - Form management
- Resend - Email service provider
- Credentials - Email/password authentication
- Google OAuth - Social authentication
- GitHub OAuth - Social authentication
- Email/password with bcrypt hashing
- Google OAuth integration
- GitHub OAuth integration
- Two-Factor Authentication (2FA) - Enhanced security for user accounts
- Email Verification - Required for new accounts
- Password Reset - Secure token-based reset flow
- User roles: ADMIN, USER
- Role gates - Component-level permission control
- Protected routes - Automatic redirect for unauthenticated users
Comprehensive Prisma schema with:
- User accounts with roles
- OAuth account linking
- Verification tokens
- Password reset tokens
- Two-factor authentication tokens
- Verification emails for new account activation
- Password reset emails with secure tokens
- Two-factor authentication emails with time-based codes
LoginForm- Handles user login with 2FA supportRegisterForm- User registration with email verificationRoleGate- Controls access based on user rolesCardWrapper- Authentication card layoutSocial- OAuth provider buttons
Button- Reusable button componentCard- Card layout componentForm- Form handling with validation
- User fills registration form
- System creates user with hashed password
- Verification email sent with secure token
- User clicks verification link to activate account
- User enters credentials
- System validates email verification status
- If 2FA enabled, sends code via email
- User enters 2FA code for final authentication
- User requests password reset
- System generates secure reset token
- Reset email sent with tokenized link
- User sets new password
- id - Unique identifier
- email - User's email address
- emailVerified - Email verification timestamp
- role - User role (ADMIN/USER)
- isTwoFactorEnabled - 2FA status
- password - Hashed password (nullable for OAuth users)
- accounts - Linked OAuth accounts
- twoFactorConfirmation - 2FA verification record
- VerificationToken - Email verification tokens
- PasswordResetToken - Password reset tokens
- TwoFactorToken - Two-factor authentication tokens
- Node.js 18+
- PostgreSQL database
- OAuth app credentials (Google, GitHub)
- Clone the repository
- Install dependencies:
npm install
- Set up environment variables:
cp .env.example .env
- Configure database:
npx prisma generate npx prisma db push
# Database
DATABASE_URL="postgresql://..."
# OAuth Providers
GOOGLE_CLIENT_ID="..."
GOOGLE_CLIENT_SECRET="..."
GITHUB_CLIENT_ID="..."
GITHUB_CLIENT_SECRET="..."
# Email Service
RESEND_API_KEY="..."
# NextAuth
NEXTAUTH_SECRET="..."
NEXT_PUBLIC_APP_URL="http://localhost:3000"# Development server
npm run dev
# Production build
npm run build
npm startmiddleware.js- Protects routes and handles authentication state
/api/auth/[...nextauth]- NextAuth.js API route handler
- Responsive design with Tailwind CSS
- Dark/light theme support with next-themes
- Form validation with Zod schemas
- Toast notifications with Sonner
- Loading states and error handling
- Access to admin dashboard
- Server action testing
- API route testing
- Verification emails - Account activation
- Password reset emails - Secure password recovery
- Two-factor authentication - Time-based verification codes
npm run dev- Start development servernpm run build- Create production build- `npm start** - Run production server
npm run lint- Run ESLint
- Database optimization with Prisma
- Token expiration - Security token management
- Error handling - Comprehensive error management
- Logging - Development and production logging
This project serves as a reference implementation for modern authentication patterns using Next.js and NextAuth.js.
MIT License - feel free to use this as a starting point for your authentication needs.
Built with β€οΈ by MH