Skip to content

Commit

Permalink
Fixes Keycloak js integration Closes #553
Browse files Browse the repository at this point in the history
  • Loading branch information
karesti committed Nov 28, 2024
1 parent 584e041 commit 459338b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 47 deletions.
12 changes: 6 additions & 6 deletions src/app/providers/UserContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -72,7 +72,7 @@ const UserContextProvider = ({ children }) => {
}, []);

useEffect(() => {
if (loadingAcl && init != 'PENDING') {
if (loadingAcl && init !== 'PENDING' && init !== 'SERVER_ERROR') {
ConsoleServices.security()
.userAcl()
.then((eitherAcl) => {
Expand All @@ -82,8 +82,8 @@ const UserContextProvider = ({ children }) => {
} else {
setError(eitherAcl.value.message);
}
setLoadingAcl(false);
});
})
.finally(() => setLoadingAcl(false));
}
}, [loadingAcl, init]);

Expand Down
57 changes: 16 additions & 41 deletions src/services/keycloakService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,21 @@ export class KeycloakService {
return this.instance;
}

public static init(configOptions: Keycloak.KeycloakConfig | undefined): Promise<void> {
public init(configOptions: Keycloak.KeycloakConfig | undefined): Promise<boolean> {
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: KeycloakError) => {
reject(errorData);
});
});
return KeycloakService.keycloakAuth
.init()
.catch((errorData: KeycloakError) => {
console.error(errorData);
return false;
})
.finally(() => {
KeycloakService.Instance.initialized = true;
});
}
}

Expand All @@ -42,21 +39,12 @@ export class KeycloakService {
return KeycloakService.keycloakAuth.authenticated ? KeycloakService.keycloakAuth.authenticated : false;
}

public login(options?: KeycloakLoginOptions): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
KeycloakService.keycloakAuth
.login(options)
.then(() => {
resolve(true);
})
.catch(() => {
reject(false);
});
});
public login(options?: KeycloakLoginOptions): Promise<void> {
return KeycloakService.keycloakAuth.login(options);
}

public logout(redirectUri?: string): void {
KeycloakService.keycloakAuth.logout();
public logout(redirectUri?: string): Promise<void> {
return KeycloakService.keycloakAuth.logout({ redirectUri: redirectUri });
}

public account(): void {
Expand All @@ -72,20 +60,7 @@ export class KeycloakService {
return KeycloakService.keycloakAuth.realm;
}

public getToken(): Promise<string> {
return new Promise<string>((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<boolean> {
return KeycloakService.keycloakAuth.updateToken(5);
}
}

0 comments on commit 459338b

Please sign in to comment.