首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的Auth-guard在没有完成可观察代码的情况下停止?

为什么我的Auth-guard在没有完成可观察代码的情况下停止?
EN

Stack Overflow用户
提问于 2020-12-23 21:54:51
回答 1查看 19关注 0票数 0

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

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

我已经把它分解成了一个测试auth-guard,去掉了角色部分,并试图找出让它工作的最佳方法。目前,似乎存在一个问题,如果我登录到IdentityServer,而不是客户端应用程序,身份验证保护永远不会完成可观察对象中的过程。

代码如下:

AuthGuard:

代码语言:javascript
复制
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:

代码语言:javascript
复制
  get isUserLoggedIn() {
    return this._isUserLoggedIn.asObservable();
  }

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

EN

回答 1

Stack Overflow用户

发布于 2020-12-24 00:58:00

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65425439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档