【问题标题】:Subscription to keycloak's logout method does not get called订阅 keycloak 的注销方法不会被调用
【发布时间】:2020-07-07 08:30:52
【问题描述】:

我在 Angular 6 项目中使用 keycloak-angular 库。 我正在尝试将 keycloakService.logout() 承诺转换为可观察的:

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

我的问题是,每当我调用此方法时,authenticationToken 都不会从 localStorage 中删除。

有什么想法吗?

【问题讨论】:

  • 很难说,取决于this.keycloakService.logout 做了什么以及它返回了什么
  • 不确定,但我认为logout() 重定向,因此当前页面上不再执行 JavaScript 代码。
  • 因此解决方案是首先从本地存储中删除,然后调用 logout 而不进行任何订阅。但这只是我的猜测。希望有使用过keyCloak经验的人发表评论。
  • 在等待一些更有用的 cmets,您可以尝试使用 .then 而不是 .subscribe 吗?它也不起作用,那么至少您知道它与转换为订阅模式无关。

标签: promise rxjs angular6 observable keycloak


【解决方案1】:

正如 trincot 所提到的,logout() 重定向,因此在当前页面上不再执行 JavaScript 代码。 解决方案是删除订阅模式并改用.then() 或异步/等待模式。

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.
  }

【讨论】:

    猜你喜欢
    • 2014-04-02
    • 2017-07-06
    • 2020-10-31
    • 2018-08-17
    • 2018-12-24
    • 2017-12-19
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多