A production-ready full-stack authentication solution with Next.js 15, Keycloak, and Auth.js (NextAuth.js v5).
- π Next.js 15 with App Router and Server Actions
- π Keycloak as Enterprise Identity Provider
- π‘οΈ Auth.js v5 for seamless session management
- ποΈ PostgreSQL with Drizzle ORM for type-safe database access
- ποΈ Terraform for automated Infrastructure as Code setup
- π³ Docker Compose for consistent development environments
- π TypeScript for full type safety
- π¨ Tailwind CSS for modern UI design
- π Secure Logout with server-side session termination
- β‘ One-Click Setup with automated installation script
# Clone repository
git clone https://github.com/yourusername/keycloak-react.git
cd keycloak-react
# Install dependencies
npm install
# Automated setup (starts Docker, configures Keycloak, initializes DB)
npm run setup
# Start development server
npm run dev
The app is now available at http://localhost:3000! π
- Username:
testuser
- Password:
Test1234!
- Node.js 18+
- Docker & Docker Compose
- npm or pnpm
- Terraform (will be installed automatically if not present)
# Environment variables are created automatically during setup
# Manual adjustments possible in .env.local
# Start all services (Keycloak + PostgreSQL)
docker-compose up -d
# Stop services
docker-compose down
# Complete reset (including database)
npm run auth:clean
# Sync schema (Development)
npm run db:push
# Run migrations (Production)
npm run db:migrate
# Open database UI
npm run db:studio
keycloak-react/
βββ src/
β βββ app/ # Next.js App Router Pages
β β βββ (auth)/ # Authenticated routes
β β βββ api/auth/ # Auth.js API endpoints
β β βββ dashboard/ # Protected area
β β βββ login/ # Login UI
β βββ components/ # React components
β β βββ auth/ # Auth-specific components
β β βββ ui/ # Reusable UI components
β βββ lib/ # Utilities and configurations
β β βββ db/ # Database schema and connection
β βββ types/ # TypeScript type definitions
β βββ auth.ts # Auth.js main configuration
βββ terraform/ # Infrastructure as Code
β βββ main.tf # Keycloak Realm & Client setup
β βββ variables.tf # Configurable parameters
βββ docker/ # Docker configurations
β βββ nginx/ # Production reverse proxy
βββ scripts/ # Automation scripts
β βββ setup-auth.sh # One-click setup script
βββ drizzle.config.ts # Database configuration
- JWT-based sessions with Auth.js
- Automatic token refresh
- Server-side session validation
- PKCE flow for OAuth 2.0
- CSRF protection
- Secure cookie handling
- Content Security Policy headers
- Single Sign-On (SSO) ready
- Multi-Factor Authentication support
- User self-service (password reset, etc.)
- Realm-based multi-tenancy
npm run dev # Start development server with Turbopack
npm run build # Create production build
npm run start # Start production server
npm run lint # Code linting
npm run typecheck # TypeScript type checking
npm run setup # Automated setup of all services
npm run auth:clean # Complete authentication reset
npm run db:push # Sync database schema
npm run db:migrate # Production migrations
npm run db:studio # Open database GUI
# Production build with optimized settings
docker build -f docker/Dockerfile.prod -t keycloak-react .
# With Docker Compose
docker-compose -f docker/docker-compose.prod.yml up -d
Important production settings:
AUTH_URL
- Public URL of the appKEYCLOAK_URL
- Keycloak server URLDATABASE_URL
- PostgreSQL connection stringAUTH_SECRET
- Generate withopenssl rand -base64 32
- URL: http://localhost:8081/admin
- Username:
admin
- Password:
admin
- Realm:
nextjs-app
- Client:
nextjs-client
- Port:
5433
(to avoid conflicts) - User:
postgres
- Password:
password
- Databases:
keycloak_db
- Keycloak metadatanextjs_app
- Application data
# Rebuild Terraform state
cd terraform
terraform destroy -auto-approve
terraform apply -auto-approve
# Check logs
docker-compose logs -f keycloak
# Check ports
lsof -i :8081
lsof -i :5433
# PostgreSQL status
docker-compose ps
# Test connection
docker exec -it keycloak-postgres psql -U postgres
- CLAUDE.md - Project conventions and AI instructions
- Auth.js Documentation
- Keycloak Documentation
- Drizzle ORM
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see LICENSE for details.
- Vercel for Next.js
- Keycloak Team
- Auth.js Contributors
- Drizzle Team for the amazing ORM