fix:
This commit is contained in:
parent
e82a0ecb5e
commit
e9735a4e99
@ -98,7 +98,7 @@ export class RequestBuilder {
|
||||
data: null,
|
||||
silenceMode: false,
|
||||
withCredentials: false
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public reset(): void {
|
||||
|
@ -73,17 +73,16 @@ export default abstract class ApiService implements SetRequestBuilderAfterBuild
|
||||
}
|
||||
|
||||
protected get combinedUrl() {
|
||||
return ApiService.addQuery(ApiService.combineUrls(this.apiUrl, AvailableVersion[this.version], this.basePath, this.request.endpoint), this.request.queryParams)
|
||||
return ApiService.addQuery(ApiService.combineUrls(this.apiUrl, AvailableVersion[this.version], this.basePath, this.request.endpoint), this.request.queryParams);
|
||||
}
|
||||
|
||||
private makeHttpRequest<Type>(method: 'get' | 'post' | 'delete' | 'put'): Observable<Type> {
|
||||
const doneEndpoint = this.combinedUrl;
|
||||
|
||||
return this.tokenRefreshService.getTokenRefreshing$().pipe(
|
||||
filter(refreshing => !refreshing),
|
||||
take(1),
|
||||
switchMap(_ => {
|
||||
return this.http.request<Type>(method, doneEndpoint, {
|
||||
filter(isRefreshing => !isRefreshing),
|
||||
switchMap(() =>
|
||||
this.http.request<Type>(method, doneEndpoint, {
|
||||
withCredentials: this.request.withCredentials,
|
||||
headers: this.request.httpHeaders,
|
||||
body: this.request.data
|
||||
@ -97,8 +96,8 @@ export default abstract class ApiService implements SetRequestBuilderAfterBuild
|
||||
this.request = RequestBuilder.getStandardRequestData();
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
})
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@ -138,7 +137,7 @@ export default abstract class ApiService implements SetRequestBuilderAfterBuild
|
||||
return this;
|
||||
|
||||
const authToken = AuthToken.httpHeader((JSON.parse(token) as AuthToken));
|
||||
authToken.keys().forEach(key => this.request.httpHeaders = this.request.httpHeaders.append(key, authToken.get(key) ?? ''))
|
||||
authToken.keys().forEach(key => this.request.httpHeaders = this.request.httpHeaders.append(key, authToken.get(key) ?? ''));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
<mat-sidenav-container class="schedule">
|
||||
<app-tabs (eventResult)="result($event)"/>
|
||||
<app-table-header [startWeek]="startWeek" [currentWeek]="currentWeek" (weekEvent)="handleWeekEvent($event)" #tableHeader/>
|
||||
<app-table-header [startWeek]="startWeek" [currentWeek]="currentWeek" (weekEvent)="handleWeekEvent($event)"
|
||||
#tableHeader/>
|
||||
<app-table [currentWeek]="currentWeek" [startWeek]="startWeek" [data]="data" [isLoad]="isLoadTable"/>
|
||||
</mat-sidenav-container>
|
||||
|
@ -61,7 +61,7 @@ export class AuthService {
|
||||
const token = localStorage.getItem(ApiService.tokenKey);
|
||||
|
||||
if (!token)
|
||||
return of();
|
||||
return of({} as TokenResponse);
|
||||
|
||||
const authToken = JSON.parse(token) as AuthToken;
|
||||
|
||||
|
@ -1,7 +1,8 @@
|
||||
import {BehaviorSubject, interval, Subscription, switchMap} from "rxjs";
|
||||
import {BehaviorSubject, filter, interval, Subscription, switchMap} from "rxjs";
|
||||
import {Injectable} from "@angular/core";
|
||||
import {AuthService} from "@service/auth.service";
|
||||
import {environment} from "@environment";
|
||||
import ApiService from "@api/api.service";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
@ -15,12 +16,12 @@ export class TokenRefreshService {
|
||||
this.setRefreshTokenExpireMs(AuthService.tokenExpiresIn.getTime() - 1000 - Date.now());
|
||||
|
||||
authService.tokenChangeError.subscribe(_ => {
|
||||
console.log('Token change error event received');
|
||||
console.debug('Token change error event received');
|
||||
this.tokenRefreshing$.next(false);
|
||||
this.stopTokenRefresh();
|
||||
});
|
||||
authService.expireTokenChange.subscribe(date => {
|
||||
console.log('Expire token change event received:', date);
|
||||
console.debug('Expire token change event received:', date);
|
||||
this.setRefreshTokenExpireMs(date.getTime() - 1000 - Date.now());
|
||||
});
|
||||
}
|
||||
@ -29,10 +30,15 @@ export class TokenRefreshService {
|
||||
if (date)
|
||||
this.refreshTokenExpireMs = new Date(date).getTime() - 1000 - Date.now();
|
||||
|
||||
if (!this.tokenRefreshSubscription || this.tokenRefreshSubscription.closed) {
|
||||
console.debug(this.tokenRefreshSubscription);
|
||||
if (this.tokenRefreshSubscription && !this.tokenRefreshSubscription.closed)
|
||||
return;
|
||||
|
||||
this.tokenRefreshSubscription = interval(this.refreshTokenExpireMs).pipe(
|
||||
filter(isRefreshing => !isRefreshing),
|
||||
switchMap(() => {
|
||||
this.tokenRefreshing$.next(true);
|
||||
console.debug('Send query to refresh token');
|
||||
return this.authService.refreshToken();
|
||||
})
|
||||
).subscribe({
|
||||
@ -40,12 +46,11 @@ export class TokenRefreshService {
|
||||
this.tokenRefreshing$.next(false);
|
||||
},
|
||||
error: error => {
|
||||
console.error('Token refresh error:', error);
|
||||
this.tokenRefreshing$.next(false);
|
||||
localStorage.removeItem(ApiService.tokenKey);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public getTokenRefreshing$(): BehaviorSubject<boolean> {
|
||||
return this.tokenRefreshing$;
|
||||
@ -60,13 +65,12 @@ export class TokenRefreshService {
|
||||
|
||||
public setRefreshTokenExpireMs(expireMs: number): void {
|
||||
if (expireMs < environment.retryDelay)
|
||||
expireMs = 3000;
|
||||
expireMs = environment.retryDelay;
|
||||
|
||||
console.log(expireMs);
|
||||
this.refreshTokenExpireMs = expireMs;
|
||||
|
||||
console.log(expireMs);
|
||||
this.stopTokenRefresh();
|
||||
this.startTokenRefresh();
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user