为什么我的 Auth-guard 没有完成可观察代码就停止了?

问题描述

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

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

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

代码如下:

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 检查中的代码运行。

解决方法

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...