Angular-oauth2-oidc tryLogin() 未按预期工作

nar*_*447 7 angular angular-oauth2-oidc

我在我的 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]
        }
      });
  }
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

nic*_*anc 0

此函数可用于确定您是否已经登录:

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

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