Keycloak
Keycloak-based authentication
OAN UI Service
A React application with Keycloak-based authentication built with Vite, TypeScript, shadcn-ui, and Tailwind CSS.
Table of Contents
Quick Start
Prerequisites
Installation
Clone the repository:
Install dependencies:
Start the development server:
Access the application:
The app will be available at the local address shown in your terminal (usually http://localhost:5173)
Important: You need a running Keycloak server and valid user credentials to access the application
Keycloak Authentication
This application uses Keycloak for authentication and authorization. Keycloak is an open-source identity and access management solution that provides features like single sign-on (SSO), social login, and user federation.
How Authentication Works
Login Required: When users visit the application, they are automatically redirected to Keycloak for authentication
Keycloak Login: Users enter their credentials on the Keycloak login page
Token Management: Keycloak issues JWT tokens that are automatically managed by the application
API Authorization: All API calls include the Bearer token for server-side authorization
Automatic Logout: Users can logout through the application, which clears their Keycloak session
Local Development Setup
1. Run Keycloak Locally with Docker
Start a Keycloak server using Docker:
Keycloak will be available at: http://localhost:8888
Admin console username/password:
admin/admin
2. Configure Keycloak
Create a Realm
Open http://localhost:8888
Login with admin credentials
Click "Create realm" or hover over the realm dropdown
Set realm name:
VistaarClick "Create"
Create a Client
Go to Clients → "Create client"
General Settings:
Client type:
OpenID ConnectClient ID:
vistaar-ui
Capability config:
Client authentication:
OFF(public client)Authorization:
OFFStandard flow:
ONDirect access grants:
ON
Login settings:
Root URL:
http://localhost:5173Home URL:
http://localhost:5173Valid redirect URIs:
http://localhost:5173/*Valid post logout redirect URIs:
http://localhost:5173/*Web origins:
http://localhost:5173
Create a User
Go to Users → "Add user"
Fill in user details:
Username:
testuserEmail:
test@example.comFirst name:
TestLast name:
User
Click "Create"
Go to "Credentials" tab
Set password and toggle "Temporary" to
OFFClick "Set password"
3. Update Application Configuration
The application is pre-configured to work with the local Keycloak setup. The configuration is in src/lib/keycloak.ts:
For local development, this should work out of the box if you followed the setup above.
Production Setup
1. Keycloak Production Setup
For production, you'll need a running Keycloak instance. Options include:
Kubernetes: Use official Keycloak operator
Docker/Container platforms: Run using Docker Compose or container orchestration
Cloud services: Use managed identity services that support OIDC
Self-hosted: Install Keycloak on your servers
2. Production Keycloak Configuration
Realm Configuration
Create a production realm (e.g.,
production-realm)Configure proper themes and login pages
Set up user federation if needed (LDAP, Active Directory, etc.)
Configure social login providers if required
Client Configuration
Client ID:
your-production-client-idSet proper redirect URIs for your production domain
Configure client scopes and roles as needed
Enable proper security settings
Security Settings
Enable HTTPS for all communications
Configure proper CORS settings
Set up rate limiting and security policies
Configure session management and token lifetimes
3. Update Application Configuration
Update src/lib/keycloak.ts for production:
4. Environment Variables (Optional)
You can use environment variables for configuration:
Then update keycloak.ts:
Configuration
Current Keycloak Settings
The application is currently configured to connect to:
URL:
https://auth-oan.kenpath.ai/authRealm:
VistaarClient ID:
vistaar-ui
Keycloak Integration Features
Automatic Login: Users are redirected to Keycloak login when not authenticated
Token Management: JWT tokens are automatically handled and refreshed
User Profile: User information is available from Keycloak token
Logout: Proper logout that clears Keycloak session
API Integration: Bearer tokens are automatically added to API requests
File Structure
Technologies Used
Vite — Build tool and development server
React — UI framework
TypeScript — Type-safe JavaScript
Keycloak — Identity and Access Management
@react-keycloak/web — React Keycloak integration
shadcn-ui — UI component library
Tailwind CSS — Utility-first CSS framework
Troubleshooting
Common Issues
1. "Loading..." screen forever
Check if Keycloak server is running and accessible
Verify the Keycloak URL in
src/lib/keycloak.tsCheck browser console for connection errors
2. "Invalid client" error
Ensure the client ID matches in Keycloak and
keycloak.tsVerify the client is properly configured in Keycloak
Check that the client is enabled
3. CORS errors
Add your application URL to "Web origins" in Keycloak client settings
Ensure "Valid redirect URIs" includes your app URL with
/*
4. Redirect loop
Check that "Valid redirect URIs" are properly configured
Ensure the application URL matches the configured URLs
Verify the realm name is correct
5. User cannot login
Verify user exists in the correct realm
Check that user is enabled
Ensure password is set correctly
Check if user has required roles/permissions
Getting Help
Check browser console for detailed error messages
Review Keycloak server logs for authentication issues
Verify all URLs and configuration match between app and Keycloak
Test with a fresh browser session to clear cached tokens
Useful Keycloak Commands
Last updated