Add Keycloak authentication to smartem app#74
Conversation
ceeb4cb to
98776aa
Compare
98776aa to
6f5a425
Compare
e24e6b3 to
8d42d2c
Compare
Add keycloak-js auth infrastructure with AuthProvider, useAuth() hook, and automatic token refresh. Wire tokens into the shared Axios interceptor so all API calls include Bearer headers when authenticated. Replace the prototyping RoleSwitcher in the Header with real auth controls (sign in / account menu / sign out). Auth is disabled by default in dev/mock mode.
8d42d2c to
f95be88
Compare
Helpdesk UASHD-4189 registered the SmartEM app against the dls realm on identity.diamond.ac.uk (prod) and identity-test.diamond.ac.uk (test) with client ID SmartEM. Replaces the placeholder master / smartem-frontend values from initial scaffolding. .env.example defaults to the test environment for local dev; the in-code fallback in config.ts stays on prod so a production build without env vars set doesn't silently point at the test realm.
Review notes — local dev exercise of the auth flowI took the branch for a spin against the DLS 1. Bug: init failure permanently bricks the login buttonIn const defaultAuth: Auth = {
initialised: false,
authenticated: false,
login: () => {}, // no-op
logout: () => {}, // no-op
getToken: () => '',
}
// …
keycloak
.init({ onLoad: 'check-sso' })
.then(() => setAuth(buildAuth(keycloak)))
.catch((err) => {
console.error('Keycloak init failed:', err)
setAuth({ ...defaultAuth, initialised: true, error: 'Failed to connect to Keycloak' })
})The keycloak instance is alive in the closure with working I hit this against Suggested fix — use the live keycloak instance so login can still redirect: .catch((err) => {
console.error('Keycloak init failed:', err)
setAuth({
...buildAuth(keycloak),
initialised: true,
error: 'Failed to connect to Keycloak',
})
})
2. Design issue:
|
Summary
keycloak-js-based auth infrastructure to thesmartemapp with a thinAuthProvider,useAuth()hook, and automatic token refresh@smartem/api) so all API calls includeAuthorization: Bearerheaders when authenticatedAuthGatewrapper — auth is disabled by default in dev/mock mode (VITE_ENABLE_MOCKS=true), enabled in productionRoleSwitcherin the dashboard Header with real Keycloak auth controls (sign in icon / account menu with sign out)Configuration
Requires env vars (see
apps/smartem/.env.example):Keycloak client registration has not been done yet — these are placeholders ready to be filled in.
Scope
This PR covers the frontend auth ceremony only. The SPA authenticates directly with Keycloak and attaches tokens to API requests. The backend does not need changes for this to work — it simply ignores the tokens until backend token validation is added separately.
Route protection and backend auth are separate concerns to be addressed in follow-up work.
Supersedes #70 (reimplemented on top of the dashboard concept from #73).
Test plan
npm run dev:smartem:mock— app loads normally, no auth UI shown (auth disabled in mock mode)VITE_AUTH_ENABLED=true npm run dev:smartemwith valid Keycloak config — login flow worksnpm run typecheckpassesnpm run build:smartemsucceedsCloses #64