Javascript 在拦截器中接收403后无法返回请求

Javascript 在拦截器中接收403后无法返回请求,javascript,angular,angular-http-interceptors,Javascript,Angular,Angular Http Interceptors,我试图截获导致403的请求(因为令牌过期),然后执行请求以获取新令牌,以便能够再次执行失败的请求 但是由于某种原因,带有新令牌的next.handle(request)没有执行被截获的请求 @Injectable() export class AuthExpiredInterceptor implements HttpInterceptor { constructor( private accountService: AccountService, private reado

我试图截获导致403的请求(因为令牌过期),然后执行请求以获取新令牌,以便能够再次执行失败的请求

但是由于某种原因,带有新令牌的next.handle(request)没有执行被截获的请求

@Injectable()
export class AuthExpiredInterceptor implements HttpInterceptor {
  constructor(
    private accountService: AccountService,
    private readonly authService: AuthService,
    private readonly router: Router
  ) {}

  intercept(
    request: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    return next.handle(request).pipe(
      catchError((error) => {
        if (error instanceof HttpErrorResponse && error.status === 403) {
          this._handle403Error(request, next);
        }
        return throwError(error);
      })
    );
  }

  _handle403Error(request: HttpRequest<any>, next: HttpHandler) {
    const refreshToken = localStorage.getItem("refreshToken");
    this.accountService.getNewToken(refreshToken).then(
      (res) => {
        if (res) {
          request = this._addToken(request, this.authService.getToken());
          return next.handle(request); // Don't perform the request intercepted 
        } else {
          this.router.navigate(["/login"]);
        }
      },
      (err) => {
        this.router.navigate(["/login"]);
      }
    );
  }

  _addToken(request: HttpRequest<any>, token: string) {
    return request.clone({
      setHeaders: {
        JWT_TOKEN: token,
      },
    });
  }
}
@Injectable()
导出类AuthExpiredInterceptor实现HttpInterceptor{
建造师(
私人会计服务:会计服务,
专用只读authService:authService,
专用只读路由器:路由器
) {}
拦截(
请求:HttpRequest,
下一步:HttpHandler
):可见{
返回next.handle(request.pipe)(
catchError((错误)=>{
if(HttpErrorResponse的错误实例&&error.status==403){
此._handle403错误(请求,下一步);
}
返回投掷器(错误);
})
);
}
_handle403Error(请求:HttpRequest,下一步:HttpHandler){
const refreshToken=localStorage.getItem(“refreshttoken”);
这个.accountService.getNewToken(刷新令牌)。然后(
(res)=>{
如果(res){
request=this.\u addToken(request,this.authService.getToken());
return next.handle(request);//不执行截获的请求
}否则{
this.router.navigate([“/login”]);
}
},
(错误)=>{
this.router.navigate([“/login”]);
}
);
}
_addToken(请求:HttpRequest,令牌:string){
return request.clone({
集合标题:{
JWT_令牌:令牌,
},
});
}
}

看起来您忘记了返回语句

拦截(
请求:HttpRequest,
下一步:HttpHandler
):可见{
返回next.handle(request.pipe)(
catchError((错误)=>{
if(HttpErrorResponse的错误实例&&error.status==403){
返回此。\u handle403错误(请求,下一步);
}
返回投掷器(错误);
})
);
}
_handle403Error(请求:HttpRequest,下一步:HttpHandler){
const refreshToken=localStorage.getItem(“refreshttoken”);
从(this.accountService.getNewToken(refreshToken)).pipe返回(
开关映射(res=>{
如果(res){
request=this.\u addToken(request,this.authService.getToken());
return next.handle(request);//不执行截获的请求
}
从(this.router.navigate([“/login”]).pipe(switchMapTo(EMPTY))返回;
}),
catchError(()=>from(this.router.navigate([“/login”])).pipe(switchMapTo(EMPTY)),
);
}

看起来您忘记了返回语句

拦截(
请求:HttpRequest,
下一步:HttpHandler
):可见{
返回next.handle(request.pipe)(
catchError((错误)=>{
if(HttpErrorResponse的错误实例&&error.status==403){
返回此。\u handle403错误(请求,下一步);
}
返回投掷器(错误);
})
);
}
_handle403Error(请求:HttpRequest,下一步:HttpHandler){
const refreshToken=localStorage.getItem(“refreshttoken”);
从(this.accountService.getNewToken(refreshToken)).pipe返回(
开关映射(res=>{
如果(res){
request=this.\u addToken(request,this.authService.getToken());
return next.handle(request);//不执行截获的请求
}
从(this.router.navigate([“/login”]).pipe(switchMapTo(EMPTY))返回;
}),
catchError(()=>from(this.router.navigate([“/login”])).pipe(switchMapTo(EMPTY)),
);
}

Updated,我构建了一个流而不是承诺。我在if(res){..}中使用断点,上面的代码无法访问它。然后你需要调试
this.accountService.getNewToken(refreshttoken)
returns。Updated,我构建了一个流而不是承诺。我在if(res){..中使用断点然后,您需要调试
this.accountService.getNewToken(refreshttoken)
返回的内容。