如何从角度拦截器的响应头中获取特定值

时间:2021-06-03 05:35:09

标签: angular http angular-http-interceptors

我正在尝试使用一个值来检查拦截器中的令牌是否已过期。令牌过期值出现在响应标头中。 screenshot of response header

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    
  const reqWithCredentials = req.clone({withCredentials: true});
  return next.handle(reqWithCredentials)
    .pipe(map(resp => {
      // on Response
      if (resp instanceof HttpResponse) {
        // Do whatever you want with the response.
        return resp;
      }
    }),
      catchError(error => {
        if (error instanceof HttpErrorResponse) {
          if (error.status === this._appConstant.ErrorCodes.Unauthorized || error.status === this._appConstant.ErrorCodes.Forbidden) {
            if (error.headers.has('Token-Expired')) {
              this.router.navigate(["/tokenexpired"]);
            }
            else {
              this.router.navigate(["/unauthorized"]);
            }
          }
          else {
            return throwError(error);
          }
        }
      })
    );
}

1 个答案:

答案 0 :(得分:0)

int