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 {HttpClient, HttpErrorResponse} from "@angular/common/http";
import {NotifyColor, OpenNotifyService} from "@service/open-notify.service"; import {NotifyColor, OpenNotifyService} from "@service/open-notify.service";
import {environment} from "@environment"; 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> { private makeHttpRequest<Type>(method: 'get' | 'post' | 'delete' | 'put', request: RequestData): Observable<Type> {
if (request.needAuth) if (request.needAuth)
return this.tokenRefreshService.getTokenRefreshing$().pipe( return this.tokenRefreshService.getTokenRefreshing$().pipe(
distinctUntilChanged(),
filter(isRefreshing => !isRefreshing), filter(isRefreshing => !isRefreshing),
first(),
switchMap(() => { switchMap(() => {
const token = localStorage.getItem(ApiService.tokenKey); const token = localStorage.getItem(ApiService.tokenKey);

View File

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