Subscription to keycloak's logout method does not get called

Viewed 568

I am using the keycloak-angular library with an Angular 6 project. I am trying to convert the keycloakService.logout() promise to an observable:

keycloakLogout(): void {
  defer(() => this.keycloakService.logout(config.frontUrl + '/home')).subscribe(() => {
    localStorage.removeItem('authenticationToken');
    this.accountService.authenticate(null)
  });
}

My problem is that whenever I call this method, authenticationToken is not removed from localStorage.

Any idea?

1 Answers

As mentioned by trincot, logout() redirects, and so no more JavaScript code is executed on the current page. The solution is to remove the subscribe pattern and use .then() instead or async/await pattern.

async keycloakLogout() {
  await this.keycloakService.logout(config.frontUrl + '/home');
  }

onLogout() {
    localStorage.removeItem('authenticationToken');
    this.accountService.authenticate(null);
    this.loginService.keycloakLogout();  // ----> here is the call of the previous method.
  }
Related