fix: unsubscribe from listening to refresh token

This commit is contained in:
Polianin Nikita 2024-08-26 01:24:57 +03:00
parent 5d79d86c44
commit 60d306f9c9
2 changed files with 6 additions and 6 deletions

View File

@ -1,4 +1,4 @@
import {catchError, filter, mergeMap, Observable, retryWhen, switchMap, timer} from "rxjs";
import {catchError, distinctUntilChanged, filter, first, mergeMap, Observable, retryWhen, switchMap, timer} from "rxjs";
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
import {NotifyColor, OpenNotifyService} from "@service/open-notify.service";
import {environment} from "@environment";
@ -94,7 +94,9 @@ export default abstract class ApiService {
private makeHttpRequest<Type>(method: 'get' | 'post' | 'delete' | 'put', request: RequestData): Observable<Type> {
if (request.needAuth)
return this.tokenRefreshService.getTokenRefreshing$().pipe(
distinctUntilChanged(),
filter(isRefreshing => !isRefreshing),
first(),
switchMap(() => {
const token = localStorage.getItem(ApiService.tokenKey);

View File

@ -26,23 +26,21 @@ export class TokenRefreshService {
}
private refreshToken(): void {
if (this.tokenRefreshing$.value) {
if (this.tokenRefreshing$.value)
return;
}
this.tokenRefreshing$.next(true);
this.authService.refreshToken()
.pipe(
catchError(error => {
catchError(_ => {
localStorage.removeItem(ApiService.tokenKey);
this.refreshTokenExpireMs = -1;
return of(undefined);
}))
.subscribe(data => {
if (data) {
if (data)
this.setRefreshTokenExpireMs(data);
}
this.tokenRefreshing$.next(false);
});
}