import type { AuthClient } from '@react-keycloak/core' ; import type { KeycloakConfig } from 'keycloak-js' ; import type { TokenPersistor } from '../persistors/types' ; import type { SSRAuthClient } from '../types' ; import { isServer } from './utils' ; let keycloakInstance ; let keycloakStubInstance ; export const getKeycloakStub = ( persistor ) : AuthClient => { const kcTokens = persistor . getTokens ( ) ; keycloakStubInstance = { init : ( ) => Promise . resolve ( true ) , updateToken : ( ) => Promise . resolve ( false ) , idToken : kcTokens . idToken , token : kcTokens . token , refreshToken : kcTokens . refreshToken , authenticated : ! ! kcTokens . idToken && ! ! kcTokens . token , } as unknown as SSRAuthClient ; return keycloakStubInstance ; } ; const Keycloak = ! isServer ( ) ? require ( 'keycloak-js' ) : null ; export const getKeycloakInstance = ( keycloakConfig , persistor ? , recreate = false ) => { const isServerCheck = isServer ( ) ; if ( recreate || ( ! keycloakInstance && ! isServerCheck ) ) { keycloakInstance = new Keycloak ( keycloakConfig ) ; } return ! isServerCheck ? keycloakInstance : getKeycloakStub ( persistor ! ) ; } ;