Skip to content

Commit 21ed058

Browse files
committed
Fixes Keycloak js integration Closes #553
1 parent 778e943 commit 21ed058

File tree

2 files changed

+22
-56
lines changed

2 files changed

+22
-56
lines changed

src/app/providers/UserContextProvider.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const UserContextProvider = ({ children }) => {
3535
if (eitherAuth.isRight()) {
3636
if (eitherAuth.value.keycloakConfig) {
3737
// Keycloak
38-
KeycloakService.init(eitherAuth.value.keycloakConfig)
38+
KeycloakService.Instance.init(eitherAuth.value.keycloakConfig)
3939
.catch((err) => {
4040
console.error(err);
4141
setInit('SERVER_ERROR');
@@ -48,8 +48,8 @@ const UserContextProvider = ({ children }) => {
4848
localStorage.setItem('react-token', KeycloakService.keycloakAuth.token as string);
4949
localStorage.setItem('react-refresh-token', KeycloakService.keycloakAuth.refreshToken as string);
5050
setTimeout(() => {
51-
KeycloakService.Instance.getToken().then((token) => {
52-
localStorage.setItem('react-token', token);
51+
KeycloakService.Instance.getToken().then((result) => {
52+
localStorage.setItem('react-token', KeycloakService.keycloakAuth.token as string);
5353
});
5454
}, 60000);
5555
setInit('DONE');
@@ -72,7 +72,7 @@ const UserContextProvider = ({ children }) => {
7272
}, []);
7373

7474
useEffect(() => {
75-
if (loadingAcl && init != 'PENDING') {
75+
if (loadingAcl && init !== 'PENDING' && init !== 'SERVER_ERROR') {
7676
ConsoleServices.security()
7777
.userAcl()
7878
.then((eitherAcl) => {
@@ -82,8 +82,8 @@ const UserContextProvider = ({ children }) => {
8282
} else {
8383
setError(eitherAcl.value.message);
8484
}
85-
setLoadingAcl(false);
86-
});
85+
})
86+
.finally(() => setLoadingAcl(false));
8787
}
8888
}, [loadingAcl, init]);
8989

src/services/keycloakService.ts

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Keycloak, { KeycloakConfig, KeycloakLoginOptions } from 'keycloak-js';
1+
import Keycloak, { KeycloakError, KeycloakLoginOptions } from 'keycloak-js';
22

33
export class KeycloakService {
44
private initialized = false;
@@ -13,24 +13,21 @@ export class KeycloakService {
1313
return this.instance;
1414
}
1515

16-
public static init(configOptions: KeycloakConfig | undefined): Promise<void> {
16+
public init(configOptions: Keycloak.KeycloakConfig | undefined): Promise<boolean> {
1717
if (!configOptions) {
1818
console.error('Unable to init Keycloak with undefined configOptions');
1919
return new Promise((resolve, reject) => reject('Unable to init Keycloak with undefined configOptions'));
2020
} else {
2121
KeycloakService.keycloakAuth = new Keycloak(configOptions);
22-
23-
return new Promise((resolve, reject) => {
24-
KeycloakService.keycloakAuth
25-
.init({})
26-
.then(() => {
27-
KeycloakService.Instance.initialized = true;
28-
resolve();
29-
})
30-
.catch((errorData) => {
31-
reject(errorData);
32-
});
33-
});
22+
return KeycloakService.keycloakAuth
23+
.init()
24+
.catch((errorData: KeycloakError) => {
25+
console.error(errorData);
26+
return false;
27+
})
28+
.finally(() => {
29+
KeycloakService.Instance.initialized = true;
30+
});
3431
}
3532
}
3633

@@ -42,30 +39,12 @@ export class KeycloakService {
4239
return KeycloakService.keycloakAuth.authenticated ? KeycloakService.keycloakAuth.authenticated : false;
4340
}
4441

45-
public login(options?: KeycloakLoginOptions): Promise<boolean> {
46-
return new Promise<boolean>((resolve, reject) => {
47-
KeycloakService.keycloakAuth
48-
.login(options)
49-
.then(() => {
50-
resolve(true);
51-
})
52-
.catch(() => {
53-
reject(false);
54-
});
55-
});
42+
public login(options?: KeycloakLoginOptions): Promise<void> {
43+
return KeycloakService.keycloakAuth.login(options);
5644
}
5745

5846
public logout(redirectUri?: string): Promise<void> {
59-
return new Promise<void>((resolve, reject) => {
60-
KeycloakService.keycloakAuth
61-
.logout({ redirectUri: redirectUri })
62-
.then(() => {
63-
resolve();
64-
})
65-
.catch(() => {
66-
reject();
67-
});
68-
});
47+
return KeycloakService.keycloakAuth.logout({ redirectUri: redirectUri });
6948
}
7049

7150
public account(): void {
@@ -81,20 +60,7 @@ export class KeycloakService {
8160
return KeycloakService.keycloakAuth.realm;
8261
}
8362

84-
public getToken(): Promise<string> {
85-
return new Promise<string>((resolve, reject) => {
86-
if (KeycloakService.keycloakAuth.token) {
87-
KeycloakService.keycloakAuth
88-
.updateToken(5)
89-
.then(() => {
90-
resolve(KeycloakService.keycloakAuth.token as string);
91-
})
92-
.catch(() => {
93-
reject('Failed to refresh token');
94-
});
95-
} else {
96-
reject('Not logged in');
97-
}
98-
});
63+
public getToken(): Promise<boolean> {
64+
return KeycloakService.keycloakAuth.updateToken(5);
9965
}
10066
}

0 commit comments

Comments
 (0)