2

我在我的 angular 8 项目中使用了 8.0.4 的发布版本和授权代码流:

这是我的代码

    this.oauthService.configure(authConfig);
    this.oauthService.tokenValidationHandler = new JwksValidationHandler();
    this.oauthService
      .loadDiscoveryDocument()
      .then(() => this.oauthService.tryLogin()) ---> [1]
      .then(() => {
        if (this.oauthService.hasValidAccessToken()) {
          return Promise.resolve();
        }else{
          this.oauthService.initCodeFlow() ---> [2]
        }
      });
  }

最初,当用户未登录时,[2] 处的代码将用户重定向到登录页面。

一旦用户提供用户名/密码并单击登录,身份提供者将使用查询字符串中的“代码”重定向回应用程序,也就是说,当我期望 [1] 处的代码使用代码登录用户时(通过将其兑换为令牌)。

相反,tryLogin() 方法不起作用,并且用户再次被重定向到无限循环中的授权端点。

请帮助我理解,这里出了什么问题。

另外,此示例是否:https ://github.com/jeroenheijmans/sample-angular-oauth2-oidc-with-auth-guards/ 适用于版本 8?

4

1 回答 1

0

此功能可用于确定您是否已经登录:

public isLoggedIn(): Promise<boolean> {
  return this.oauthService.loadDiscoveryDocument('<your discoveryDocumentUrl>').then(() => {
    return this.oauthService.silentRefresh().then(() => true).catch(() => false);
  });
}

参考:https ://manfredsteyer.github.io/angular-oauth2-oidc/docs/additional-documentation/silent-refresh.html

于 2021-05-11T13:36:36.463 回答