From e9735a4e99d88fb2287b89f5f2e13ec07be3d3ce Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sun, 4 Aug 2024 23:03:06 +0300 Subject: [PATCH] fix: --- src/api/RequestBuilder.ts | 2 +- src/api/api.service.ts | 15 ++++--- src/api/v1/schedule.service.ts | 8 ++-- src/app/app.component.ts | 2 +- src/app/app.config.ts | 8 ++-- src/pages/schedule/schedule.component.html | 3 +- src/services/auth.service.ts | 2 +- src/services/token-refresh.service.ts | 48 ++++++++++++---------- 8 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/api/RequestBuilder.ts b/src/api/RequestBuilder.ts index c6b62ca..74ffec3 100644 --- a/src/api/RequestBuilder.ts +++ b/src/api/RequestBuilder.ts @@ -98,7 +98,7 @@ export class RequestBuilder { data: null, silenceMode: false, withCredentials: false - } + }; } public reset(): void { diff --git a/src/api/api.service.ts b/src/api/api.service.ts index f2ebf95..a8bf86a 100644 --- a/src/api/api.service.ts +++ b/src/api/api.service.ts @@ -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(method: 'get' | 'post' | 'delete' | 'put'): Observable { const doneEndpoint = this.combinedUrl; return this.tokenRefreshService.getTokenRefreshing$().pipe( - filter(refreshing => !refreshing), - take(1), - switchMap(_ => { - return this.http.request(method, doneEndpoint, { + filter(isRefreshing => !isRefreshing), + switchMap(() => + this.http.request(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; } diff --git a/src/api/v1/schedule.service.ts b/src/api/v1/schedule.service.ts index a0d6ae4..ffb6a3e 100644 --- a/src/api/v1/schedule.service.ts +++ b/src/api/v1/schedule.service.ts @@ -26,7 +26,7 @@ export class ScheduleService extends ApiService { .post(); } - public getByGroup(id : number, isEven: boolean | null = null, disciplines: Array | null = null, professors: Array | null = null, lectureHalls: Array | null = null) { + public getByGroup(id: number, isEven: boolean | null = null, disciplines: Array | null = null, professors: Array | null = null, lectureHalls: Array | null = null) { return this.createRequestBuilder() .setEndpoint('GetByGroup/' + id.toString()) .setQueryParams({isEven: isEven, disciplines: disciplines, professors: professors, lectureHalls: lectureHalls}) @@ -34,7 +34,7 @@ export class ScheduleService extends ApiService { .get(); } - public getByProfessor(id : number, isEven: boolean | null = null, disciplines: Array | null = null, groups: Array | null = null, lectureHalls: Array | null = null) { + public getByProfessor(id: number, isEven: boolean | null = null, disciplines: Array | null = null, groups: Array | null = null, lectureHalls: Array | null = null) { return this.createRequestBuilder() .setEndpoint('GetByProfessor/' + id.toString()) .setQueryParams({isEven: isEven, disciplines: disciplines, groups: groups, lectureHalls: lectureHalls}) @@ -42,7 +42,7 @@ export class ScheduleService extends ApiService { .get(); } - public getByLectureHall(id : number, isEven: boolean | null = null, disciplines: Array | null = null, groups: Array | null = null, professors: Array | null = null) { + public getByLectureHall(id: number, isEven: boolean | null = null, disciplines: Array | null = null, groups: Array | null = null, professors: Array | null = null) { return this.createRequestBuilder() .setEndpoint('GetByLectureHall/' + id.toString()) .setQueryParams({isEven: isEven, disciplines: disciplines, groups: groups, professors: professors}) @@ -50,7 +50,7 @@ export class ScheduleService extends ApiService { .get(); } - public getByDiscipline(id : number, isEven: boolean | null = null, groups: Array | null = null, professors: Array | null = null, lectureHalls: Array | null = null) { + public getByDiscipline(id: number, isEven: boolean | null = null, groups: Array | null = null, professors: Array | null = null, lectureHalls: Array | null = null) { return this.createRequestBuilder() .setEndpoint('GetByDiscipline/' + id.toString()) .setQueryParams({isEven: isEven, groups: groups, professors: professors, lectureHalls: lectureHalls}) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index b13707f..1c8cb8a 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -2,7 +2,7 @@ import {Component} from '@angular/core'; import {RouterOutlet} from '@angular/router'; import {FooterComponent} from "@component/common/footer/footer.component"; import localeRu from '@angular/common/locales/ru'; -import { registerLocaleData } from '@angular/common'; +import {registerLocaleData} from '@angular/common'; import {FocusNextDirective} from "@/directives/focus-next.directive"; import {TokenRefreshService} from "@service/token-refresh.service"; diff --git a/src/app/app.config.ts b/src/app/app.config.ts index a07ad01..c142778 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -1,8 +1,8 @@ -import { ApplicationConfig } from '@angular/core'; -import { provideRouter } from '@angular/router'; +import {ApplicationConfig} from '@angular/core'; +import {provideRouter} from '@angular/router'; -import { routes } from './app.routes'; -import { provideAnimationsAsync } from '@angular/platform-browser/animations/async'; +import {routes} from './app.routes'; +import {provideAnimationsAsync} from '@angular/platform-browser/animations/async'; import {provideHttpClient} from "@angular/common/http"; export const appConfig: ApplicationConfig = { diff --git a/src/pages/schedule/schedule.component.html b/src/pages/schedule/schedule.component.html index 5ffe3ac..c9f787f 100644 --- a/src/pages/schedule/schedule.component.html +++ b/src/pages/schedule/schedule.component.html @@ -1,5 +1,6 @@ - + diff --git a/src/services/auth.service.ts b/src/services/auth.service.ts index 0a23f37..b153807 100644 --- a/src/services/auth.service.ts +++ b/src/services/auth.service.ts @@ -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; diff --git a/src/services/token-refresh.service.ts b/src/services/token-refresh.service.ts index b99996d..ad8bd1f 100644 --- a/src/services/token-refresh.service.ts +++ b/src/services/token-refresh.service.ts @@ -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,22 +30,26 @@ export class TokenRefreshService { if (date) this.refreshTokenExpireMs = new Date(date).getTime() - 1000 - Date.now(); - if (!this.tokenRefreshSubscription || this.tokenRefreshSubscription.closed) { - this.tokenRefreshSubscription = interval(this.refreshTokenExpireMs).pipe( - switchMap(() => { - this.tokenRefreshing$.next(true); - return this.authService.refreshToken(); - }) - ).subscribe({ - next: (_) => { - this.tokenRefreshing$.next(false); - }, - error: error => { - console.error('Token refresh error:', error); - this.tokenRefreshing$.next(false); - } - }); - } + 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({ + next: (_) => { + this.tokenRefreshing$.next(false); + }, + error: error => { + this.tokenRefreshing$.next(false); + localStorage.removeItem(ApiService.tokenKey); + } + }); } public getTokenRefreshing$(): BehaviorSubject { @@ -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(); } } -