NextAuth.js with Keycloak Demo
This is a Next.js application demonstrating authentication using NextAuth.js with Keycloak as the identity provider.
Features
- š NextAuth.js integration with Keycloak
- šØ Modern UI with Tailwind CSS
- š± Responsive design
- š Session management
- š TypeScript support
Prerequisites
- Node.js 18+ and pnpm
- A running Keycloak instance
- A configured Keycloak client
Setup
1. Install Dependencies
pnpm install
2. Environment Variables
Create a .env.local file in the root directory with the following variables:
# NextAuth Configuration
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=your-nextauth-secret-key-here
# Keycloak Configuration
KEYCLOAK_CLIENT_ID=your-client-id
KEYCLOAK_CLIENT_SECRET=your-client-secret
KEYCLOAK_ISSUER=https://your-keycloak-domain/auth/realms/your-realm
3. Keycloak Configuration
-
Create a Realm (if you don't have one):
- Log into your Keycloak admin console
- Create a new realm or use an existing one
-
Create a Client:
- Go to Clients ā Create
- Set Client ID (e.g.,
nextauth-demo) - Set Client Protocol to
openid-connect - Set Access Type to
confidential - Add Valid Redirect URIs:
http://localhost:3000/api/auth/callback/keycloakhttp://localhost:3000/auth/signin
- Add Web Origins:
http://localhost:3000
-
Get Client Credentials:
- Go to the Credentials tab of your client
- Copy the Client Secret
-
Configure Client Scopes:
- Go to the Client Scopes tab
- Add the following scopes:
openidprofileemail
4. Update Environment Variables
Replace the placeholder values in .env.local with your actual Keycloak configuration:
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=your-super-secret-key-here
KEYCLOAK_CLIENT_ID=nextauth-demo
KEYCLOAK_CLIENT_SECRET=your-actual-client-secret
KEYCLOAK_ISSUER=https://your-keycloak-domain/auth/realms/your-realm
5. Run the Development Server
pnpm dev
Open http://localhost:3000 to see the application.
Usage
- Sign In: Click the "Sign In with Keycloak" button
- Authentication: You'll be redirected to Keycloak for authentication
- User Info: After successful authentication, you'll see your user information
- Sign Out: Click the "Sign Out" button to end your session
Project Structure
src/
āāā app/
ā āāā api/auth/[...nextauth]/route.ts # NextAuth API route
ā āāā auth/signin/page.tsx # Custom sign-in page
ā āāā layout.tsx # Root layout with SessionProvider
ā āāā page.tsx # Main page with auth demo
āāā components/
ā āāā auth/
ā ā āāā SignInButton.tsx # Sign-in button component
ā ā āāā SignOutButton.tsx # Sign-out button component
ā āāā providers/
ā āāā SessionProvider.tsx # NextAuth session provider
āāā lib/
ā āāā auth.ts # NextAuth configuration
āāā types/
āāā next-auth.d.ts # TypeScript type extensions
Keycloak Setup Details
Client Configuration
Your Keycloak client should have these settings:
- Client ID:
nextauth-demo(or your preferred name) - Client Protocol:
openid-connect - Access Type:
confidential - Valid Redirect URIs:
http://localhost:3000/api/auth/callback/keycloakhttp://localhost:3000/auth/signin
- Web Origins:
http://localhost:3000
User Creation
- Go to Users ā Add User
- Fill in the required fields (Username, Email, First Name, Last Name)
- Go to the Credentials tab and set a password
- Make sure the user is enabled
Troubleshooting
Common Issues
-
"Invalid redirect URI" error:
- Check that your redirect URIs in Keycloak match exactly
- Ensure the protocol (http/https) matches
-
"Client not found" error:
- Verify your client ID and secret are correct
- Check that the client is enabled in Keycloak
-
"Realm not found" error:
- Verify your realm name in the issuer URL
- Check that the realm exists and is active
-
Session not persisting:
- Ensure
NEXTAUTH_SECRETis set and unique - Check that cookies are enabled in your browser
- Ensure
Debug Mode
To enable debug logging, add this to your .env.local:
DEBUG=next-auth:*
Production Deployment
For production deployment:
- Update
NEXTAUTH_URLto your production domain - Update Keycloak redirect URIs to include your production domain
- Use a strong, unique
NEXTAUTH_SECRET - Consider using environment-specific Keycloak realms
Learn More
License
This project is open source and available under the MIT License.