diff --git a/src/app/providers/UserContextProvider.tsx b/src/app/providers/UserContextProvider.tsx index eb429d7b..67196b49 100644 --- a/src/app/providers/UserContextProvider.tsx +++ b/src/app/providers/UserContextProvider.tsx @@ -35,7 +35,7 @@ const UserContextProvider = ({ children }) => { if (eitherAuth.isRight()) { if (eitherAuth.value.keycloakConfig) { // Keycloak - KeycloakService.init(eitherAuth.value.keycloakConfig) + KeycloakService.Instance.init(eitherAuth.value.keycloakConfig) .catch((err) => { console.error(err); setInit('SERVER_ERROR'); @@ -48,8 +48,8 @@ const UserContextProvider = ({ children }) => { localStorage.setItem('react-token', KeycloakService.keycloakAuth.token as string); localStorage.setItem('react-refresh-token', KeycloakService.keycloakAuth.refreshToken as string); setTimeout(() => { - KeycloakService.Instance.getToken().then((token) => { - localStorage.setItem('react-token', token); + KeycloakService.Instance.getToken().then((result) => { + localStorage.setItem('react-token', KeycloakService.keycloakAuth.token as string); }); }, 60000); setInit('DONE'); @@ -72,7 +72,7 @@ const UserContextProvider = ({ children }) => { }, []); useEffect(() => { - if (loadingAcl && init != 'PENDING') { + if (loadingAcl && init !== 'PENDING' && init !== 'SERVER_ERROR') { ConsoleServices.security() .userAcl() .then((eitherAcl) => { @@ -82,8 +82,8 @@ const UserContextProvider = ({ children }) => { } else { setError(eitherAcl.value.message); } - setLoadingAcl(false); - }); + }) + .finally(() => setLoadingAcl(false)); } }, [loadingAcl, init]); diff --git a/src/services/keycloakService.ts b/src/services/keycloakService.ts index 62c9deff..8b1962b2 100644 --- a/src/services/keycloakService.ts +++ b/src/services/keycloakService.ts @@ -1,4 +1,4 @@ -import Keycloak, { KeycloakConfig, KeycloakLoginOptions } from 'keycloak-js'; +import Keycloak, { KeycloakError, KeycloakLoginOptions } from 'keycloak-js'; export class KeycloakService { private initialized = false; @@ -13,24 +13,21 @@ export class KeycloakService { return this.instance; } - public static init(configOptions: KeycloakConfig | undefined): Promise { + public init(configOptions: Keycloak.KeycloakConfig | undefined): Promise { if (!configOptions) { console.error('Unable to init Keycloak with undefined configOptions'); return new Promise((resolve, reject) => reject('Unable to init Keycloak with undefined configOptions')); } else { KeycloakService.keycloakAuth = new Keycloak(configOptions); - - return new Promise((resolve, reject) => { - KeycloakService.keycloakAuth - .init({}) - .then(() => { - KeycloakService.Instance.initialized = true; - resolve(); - }) - .catch((errorData) => { - reject(errorData); - }); - }); + return KeycloakService.keycloakAuth + .init() + .catch((errorData: KeycloakError) => { + console.error(errorData); + return false; + }) + .finally(() => { + KeycloakService.Instance.initialized = true; + }); } } @@ -42,30 +39,12 @@ export class KeycloakService { return KeycloakService.keycloakAuth.authenticated ? KeycloakService.keycloakAuth.authenticated : false; } - public login(options?: KeycloakLoginOptions): Promise { - return new Promise((resolve, reject) => { - KeycloakService.keycloakAuth - .login(options) - .then(() => { - resolve(true); - }) - .catch(() => { - reject(false); - }); - }); + public login(options?: KeycloakLoginOptions): Promise { + return KeycloakService.keycloakAuth.login(options); } public logout(redirectUri?: string): Promise { - return new Promise((resolve, reject) => { - KeycloakService.keycloakAuth - .logout({ redirectUri: redirectUri }) - .then(() => { - resolve(); - }) - .catch(() => { - reject(); - }); - }); + return KeycloakService.keycloakAuth.logout({ redirectUri: redirectUri }); } public account(): void { @@ -81,20 +60,7 @@ export class KeycloakService { return KeycloakService.keycloakAuth.realm; } - public getToken(): Promise { - return new Promise((resolve, reject) => { - if (KeycloakService.keycloakAuth.token) { - KeycloakService.keycloakAuth - .updateToken(5) - .then(() => { - resolve(KeycloakService.keycloakAuth.token as string); - }) - .catch(() => { - reject('Failed to refresh token'); - }); - } else { - reject('Not logged in'); - } - }); + public getToken(): Promise { + return KeycloakService.keycloakAuth.updateToken(5); } }