响应后刷新令牌-视图未更新/所有请求未触发-HttpInterceptor

时间:2019-03-15 13:51:23

标签: angular angular-http-interceptors refresh-token

我在刷新令牌中遇到问题,一旦获得新令牌,就发送请求并获得响应,但视图未更新。而且所有请求均未发送,仅发送了少量请求。

由于令牌过期时错误响应为200。请使用“轻按”代替“捕获错误”,请查找下面的代码,我们将不胜感激

 intercept(request: HttpRequest<any>, next: HttpHandler):
    Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any> | any> {
    return next.handle(this.addTokenToRequest(request, this.authService.getToken())).pipe(
      tap(event => {
          if (errorCode === 'E101') {
            return <any>this.handle401Error(request, next);
          }
      })
    );

}
 handle401Error(request: HttpRequest<any>, next: HttpHandler) {
    if (!this.isRefreshToken) {
      this.isRefreshToken = true;
      this.tokenSubject.next(null);

      return this.LoginService.refreshToken().pipe(
        switchMap(user => {
          if (user) {
            this.tokenSubject.next(user.token);
            this.authService.loggedIn.next(null);
            this.authService.loggedIn.next(user);
            return next.handle(this.addTokenToRequest(request, user.token));
          }
          return <any>this.authService.logout();
        }),
        catchError(err => {
          return <any>this.authService.logout();
        }),
        finalize(() => {
          this.isRefreshToken = false;
        })
      ).subscribe();
    } else {
      this.isRefreshToken = false;
      console.log(this.tokenSubject, 'tokenSubject');
      return this.tokenSubject.pipe(
        filter(token => token != null),
        take(1),
        switchMap(token => {
          return next.handle(this.addTokenToRequest(request, token));
        })
      );
    }
  }

0 个答案:

没有答案