1- import Keycloak , { KeycloakConfig , KeycloakLoginOptions } from 'keycloak-js' ;
1+ import Keycloak , { KeycloakError , KeycloakLoginOptions } from 'keycloak-js' ;
22
33export 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