【问题标题】:Why does my Auth-guard stop without completing the observable code?为什么我的 Auth-guard 在未完成可观察代码的情况下停止?
【发布时间】:2020-12-23 13:54:51
【问题描述】:

我有一个应用程序,如果他们已经通过我们的任何其他应用程序登录到提供我的身份验证的 IdentityServer 项目,我需要使用 Auth-Guards 执行静默更新调用以允许他们通过。基本上,我们希望在应用程序之间实现无缝登录流程。

目前,我正在处理或尝试的方式是在每个页面上都有一个身份验证保护,对于某些不需要身份验证但可以使用它来处理对服务器的角色请求的页面,我会检查它,执行静默更新以防万一,然后获取角色(如果它们已通过身份验证),否则仍返回 true。

我已将其分解为测试 auth-guard,删除了角色位,并试图找出使其工作的最佳方法。目前,如果我登录到 IdentityServer 而不是客户端应用程序,似乎存在一个问题,即身份验证守卫永远不会完成可观察对象内的过程。

代码如下:

AuthGuard:

canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot,
  ): Observable<boolean> | Promise<boolean | UrlTree> | boolean {
    this.spinner.start();//this blocks the screen until it is done, it is what told me that nothing 
    //else was getting hit
    return this.oidcAuthService.isUserLoggedIn.pipe(
      delay(1000),
      map(loggedIn => {
        if (!loggedIn) {
          this.oidcAuthService.renewToken().then(user => {
            if (user && !user.expired) {
              return true;
            } else {
              return this.accessDenied();
            }
          }).catch(error => {
            if (error instanceof HttpErrorResponse) {
              if (error.message === 'login_required') {
                return this.accessDenied();
              }
              return this.accessDenied();
            }
          })
        } else {
          return true;
        }
      }),
    );
}  

private accessDenied() {
    window.alert("You don't have permission to view this page");

    this.router.navigate(['/']);

    return false;
  }

...

OidcAuthService:

  get isUserLoggedIn() {
    return this._isUserLoggedIn.asObservable();
  }

问题是,如果客户端未登录而服务器已登录,我似乎无法运行 isUserLogged 检查中的代码。

【问题讨论】:

    标签: angular identityserver4 auth-guard


    【解决方案1】:

    不要使用 .pipe() 而是使用 .subscribe()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多