angular-oauth2-oidc tryLogin()无法正常工作

发布时间:2020-07-07 16:37

我在带有授权代码流的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?

回答1

这个函数可以用来判断你是否已经登录:

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