Я сталкиваюсь с довольно близкой проблемой, и я решаю ее с помощью 'switchmap' в 'pipe' с помощью HttpInterceptor, например так:
HttpErrorFilter:
import { BehaviorSubject, Observable, of, throwError } from 'rxjs';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { catchError, filter, finalize, switchMap, take } from 'rxjs/operators';
import { AuthenticationService } from './../services/authentication.service';
import { Injectable } from '@angular/core';
@Injectable()
export class HttpErrorFilter implements HttpInterceptor {
isRefreshingToken = false;
tokenSubject: BehaviorSubject<string> = new BehaviorSubject<string>(null);
constructor(private authenticationService: AuthenticationService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
catchError((error: any) => {
if (error.status === 401 && !request.url.includes('auth/signin')) {
return this.handle401Error(request, next);
}
return throwError(error);
})
);
}
private handle401Error(request: HttpRequest<any>, next: HttpHandler) {
if (request.url.includes('auth/refreshtoken')) {
this.isRefreshingToken = false;
return of(<any>this.authenticationService.logout());
}
if (!this.isRefreshingToken) {
this.isRefreshingToken = true;
this.tokenSubject.next(null);
return this.authenticationService.refresh().pipe(switchMap(token => {
if (token) {
this.tokenSubject.next(token.value);
return next.handle(request);
}
return of(<any>this.authenticationService.logout());
}),
catchError(err => {
this.authenticationService.logout();
return throwError(err.error);
}),
finalize(() => {
this.isRefreshingToken = false;
}));
} else {
this.isRefreshingToken = false;
return this.tokenSubject
.pipe(filter(token => token != null),
take(1),
switchMap(token => {
return next.handle(request);
}));
}
}
}
HttpFilter:
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from './../../../environments/environment';
@Injectable()
export class HttpFilter implements HttpInterceptor {
private apiUrl = environment.apiUrl;
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(this.addAuthentication(request));
}
private addAuthentication(request: HttpRequest<any>): HttpRequest<any> {
if (!request.url.includes('/auth/')) {
const token = localStorage.getItem('access.token');
if (token) {
request = request.clone({
setHeaders: {Authorization: 'Bearer ' + token}
});
}
}
return request.clone({url: `${this.apiUrl}${request.url}`});
}
}
NgModule обеспечивает:
@NgModule({
...
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: HttpErrorFilter, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: HttpFilter, multi: true },
...
],
...
})