diff --git a/src/app/app.component.ts b/src/app/app.component.ts index c8ffa87..f578151 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -78,13 +78,6 @@ export class AppComponent implements OnInit { } }); } - console.log('Starting authorisation refresh'); - this.authorizationRequestService.refresh().finally(null); - console.log('Set interval for refresh routine'); - setInterval( - () => this.authorizationRequestService.refresh().finally(null), - 60000, - ); router.events.subscribe((event) => { if (event instanceof NavigationStart) { @@ -94,6 +87,7 @@ export class AppComponent implements OnInit { } async ngOnInit(): Promise { + await this.authorizationRequestService.startRefresh(); if (window.localStorage.getItem('allow-functional-cookies')) { this.functionalCookiesAllowed$.next(true); } diff --git a/src/app/services/backend/request/authorization-request.service.ts b/src/app/services/backend/request/authorization-request.service.ts index c688607..2fbecb5 100644 --- a/src/app/services/backend/request/authorization-request.service.ts +++ b/src/app/services/backend/request/authorization-request.service.ts @@ -66,22 +66,26 @@ export class AuthorizationRequestService { this.authorized$.next(response); } - async refresh(): Promise { - try { - console.log('Refreshing authorisation'); - const response = await firstValueFrom( - this.http.get('/api/authorization/v1/refresh'), - ); - if (this.authorized$.getValue() !== response) { - this.authorized$.next(response); - } - } catch (error: any) { - if (error instanceof HttpErrorResponse && error.status === 401) { - // Remove authorization as a precaution for the user, something went severely wrong - this.authorized$.next(null); - this.router.navigate(['/']); - } else { - throw error; + async refresh(force = false): Promise { + if (force || this.authorized$.value) { + try { + console.log('Refreshing authorisation'); + const response = await firstValueFrom( + this.http.get('/api/authorization/v1/refresh'), + ); + if (this.authorized$.getValue() !== response) { + this.authorized$.next(response); + } + } catch (error: any) { + if (error instanceof HttpErrorResponse && error.status === 401) { + if (this.authorized$.getValue()) { + // Remove authorization as a precaution for the user, something went severely wrong + this.authorized$.next(null); + await this.router.navigate(['/']); + } + } else { + throw error; + } } } } @@ -90,4 +94,9 @@ export class AuthorizationRequestService { await firstValueFrom(this.http.get('/api/authorization/v1/logout')); this.authorized$.next(null); } + + async startRefresh() { + this.refresh(true).finally(null); + setInterval(() => this.refresh().finally(null), 60000); + } }