|
| 1 | +import React, { |
| 2 | + createContext, |
| 3 | + useContext, |
| 4 | + useEffect, |
| 5 | + useRef, |
| 6 | + useState |
| 7 | +} from 'react'; |
| 8 | +import Keycloak from 'keycloak-js'; |
| 9 | + |
| 10 | +const url = process.env.REACT_APP_KEYCLOAK_URL; |
| 11 | +const realm = process.env.REACT_APP_KEYCLOAK_REALM; |
| 12 | +const clientId = process.env.REACT_APP_KEYCLOAK_CLIENT_ID; |
| 13 | + |
| 14 | +const isAuthEnabled = !!(url && realm && clientId); |
| 15 | + |
| 16 | +const keycloak: Keycloak | undefined = isAuthEnabled |
| 17 | + ? new (Keycloak as any)({ |
| 18 | + url, |
| 19 | + realm, |
| 20 | + clientId |
| 21 | + }) |
| 22 | + : undefined; |
| 23 | + |
| 24 | +interface UserProfile { |
| 25 | + groups: string[]; |
| 26 | + username: string; |
| 27 | +} |
| 28 | + |
| 29 | +export type KeycloakContextProps = { |
| 30 | + initStatus: 'loading' | 'success' | 'error'; |
| 31 | + isLoading: boolean; |
| 32 | + profile?: UserProfile; |
| 33 | + hasDPADAccess: boolean; |
| 34 | +} & ( |
| 35 | + | { |
| 36 | + keycloak: Keycloak; |
| 37 | + isEnabled: true; |
| 38 | + } |
| 39 | + | { |
| 40 | + keycloak: undefined; |
| 41 | + isEnabled: false; |
| 42 | + } |
| 43 | +); |
| 44 | + |
| 45 | +const KeycloakContext = createContext<KeycloakContextProps>({ |
| 46 | + initStatus: 'loading', |
| 47 | + isEnabled: isAuthEnabled |
| 48 | +} as KeycloakContextProps); |
| 49 | + |
| 50 | +export const KeycloakProvider = (props: { children: React.ReactNode }) => { |
| 51 | + const [initStatus, setInitStatus] = |
| 52 | + useState<KeycloakContextProps['initStatus']>('loading'); |
| 53 | + const [profile, setProfile] = useState<UserProfile | undefined>(); |
| 54 | + |
| 55 | + const wasInit = useRef(false); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + async function initialize() { |
| 59 | + if (!keycloak) return; |
| 60 | + // Keycloak can only be initialized once. This is a workaround to avoid |
| 61 | + // multiple initialization attempts, specially by React double rendering. |
| 62 | + if (wasInit.current) return; |
| 63 | + wasInit.current = true; |
| 64 | + |
| 65 | + try { |
| 66 | + await keycloak.init({ |
| 67 | + // onLoad: 'login-required', |
| 68 | + onLoad: 'check-sso', |
| 69 | + checkLoginIframe: false |
| 70 | + }); |
| 71 | + if (keycloak.authenticated) { |
| 72 | + // const profile = |
| 73 | + // await (keycloak.loadUserProfile() as unknown as Promise<Keycloak.KeycloakProfile>); |
| 74 | + setProfile({ |
| 75 | + groups: keycloak.idTokenParsed?.access_group || [], |
| 76 | + username: keycloak.idTokenParsed?.preferred_username || '' |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + setInitStatus('success'); |
| 81 | + } catch (err) { |
| 82 | + setInitStatus('error'); |
| 83 | + // eslint-disable-next-line no-console |
| 84 | + console.error('Failed to initialize keycloak adapter:', err); |
| 85 | + } |
| 86 | + } |
| 87 | + initialize(); |
| 88 | + }, []); |
| 89 | + |
| 90 | + const base = { |
| 91 | + initStatus, |
| 92 | + isLoading: isAuthEnabled && initStatus === 'loading', |
| 93 | + profile, |
| 94 | + hasDPADAccess: !!( |
| 95 | + isAuthEnabled && |
| 96 | + keycloak?.authenticated && |
| 97 | + profile?.groups?.includes('DPAD_Direct_Access') |
| 98 | + ) |
| 99 | + }; |
| 100 | + |
| 101 | + return ( |
| 102 | + <KeycloakContext.Provider |
| 103 | + value={ |
| 104 | + isAuthEnabled |
| 105 | + ? { |
| 106 | + ...base, |
| 107 | + keycloak: keycloak!, |
| 108 | + isEnabled: true |
| 109 | + } |
| 110 | + : { |
| 111 | + ...base, |
| 112 | + keycloak: undefined, |
| 113 | + isEnabled: false |
| 114 | + } |
| 115 | + } |
| 116 | + > |
| 117 | + {props.children} |
| 118 | + </KeycloakContext.Provider> |
| 119 | + ); |
| 120 | +}; |
| 121 | + |
| 122 | +export const useKeycloak = () => { |
| 123 | + const ctx = useContext(KeycloakContext); |
| 124 | + |
| 125 | + if (!ctx) { |
| 126 | + throw new Error('useKeycloak must be used within a KeycloakProvider'); |
| 127 | + } |
| 128 | + |
| 129 | + return ctx; |
| 130 | +}; |
0 commit comments