How dasniko/keycloak-reactjs-demo works
Project Type and Purpose
• This is a front-end demo application (“Keycloak React.JS Demo”) showing how to integrate a React/Vite single-page app with Keycloak for IAM/SSO without extra libraries.
• It fetches “books” data from a backend (AWS Lambda or any REST service defined by backend-swagger-spec.yml) and demonstrates conditional rendering based on authentication state and roles.
File Structure Analysis
• Top-level files: index.html, vite.config.js, package.json, deploy.sh
• public/ holds Keycloak adapter config (keycloak.json) and the silent SSO check HTML.
• src/ contains:
• components/ – React UI components (App.jsx, BookList.jsx, conditional renderers, etc.)
• modules/ – API wrappers (books.js)
• services/ – user session handling (UserService.js)
• deployment/ holds Dockerfile and Nginx config (bookbox.conf) for production packaging.
• backend-swagger-spec.yml describes the REST API contract.
Key README Insights
• Uses React ≥18, Vite, React Router v6, and the official keycloak-js adapter.
• No custom server code here; backend is external (AWS Lambda or any service matching the Swagger spec).
• Deployment expects a reverse-proxy/static host (via Nginx in Docker) serving the SPA and forwarding /api calls.
Main Components
a. Client (Browser)
• React SPA (built with Vite)
• UI Components (App, BookList, BookDetails, BookForm, SecretBooks, etc.)
• React Router v6 for navigation
• keycloak-js adapter for authentication flows
• modules/books.js → HTTP client to /api/books
• services/UserService.js → wraps Keycloak token/session operations
b. External IAM Service
• Keycloak Server (Identity Provider)
c. Backend API Service
• REST endpoints (AWS Lambda or custom service defined by backend-swagger-spec.yml)
• Secured via Bearer tokens issued by Keycloak
d. Deployment Infrastructure
• Nginx (configured by bookbox.conf) serving static files, proxying /api → backend
• Docker container packaging the Nginx + built SPA
Relationships and Data Flows
• User opens the SPA in the browser
• SPA (keycloak-js) redirects to Keycloak for login
• On successful login, Keycloak redirects back with tokens
• UserService stores/accesses tokens, exposes user info & roles
• UI components (RenderOnAuthenticated/Role) read UserService → conditional rendering
• For data fetches, modules/books.js attaches Bearer token → calls Nginx /api/books
• Nginx proxies /api → Backend API → responds with JSON data
Architectural Patterns & Design Principles
• Layered Architecture: UI layer → service/module layer → transport layer
• Separation of Concerns: React components do not handle auth logic directly (delegated to keycloak-js + UserService)
• Declarative Routing & Conditional Rendering based on auth state/roles
• Infrastructure-as-Code: Dockerfile + Nginx config for reproducible deployments
Technologies & Frameworks
• React (Presentation)
• Vite (Build & dev server)
• React Router v6 (Routing)
• keycloak-js (Auth adapter)
• Axios/fetch (in modules/books.js)
• Nginx + Docker (Deployment)
• Swagger (API contract)
• AWS Lambda (Optional backend hosting)