Skip to content

Commit

Permalink
Made token refresh smarter
Browse files Browse the repository at this point in the history
* Don't attempt to refresh tokens when it is not necessary
* Start the refresh sequence within the refresh service
  • Loading branch information
santidhammo committed Jan 8, 2025
1 parent 543913b commit 2caf01f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
8 changes: 1 addition & 7 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -94,6 +87,7 @@ export class AppComponent implements OnInit {
}

async ngOnInit(): Promise<void> {
await this.authorizationRequestService.startRefresh();
if (window.localStorage.getItem('allow-functional-cookies')) {
this.functionalCookiesAllowed$.next(true);
}
Expand Down
41 changes: 25 additions & 16 deletions src/app/services/backend/request/authorization-request.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,26 @@ export class AuthorizationRequestService {
this.authorized$.next(response);
}

async refresh(): Promise<void> {
try {
console.log('Refreshing authorisation');
const response = await firstValueFrom(
this.http.get<AuthorizationResponse>('/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<void> {
if (force || this.authorized$.value) {
try {
console.log('Refreshing authorisation');
const response = await firstValueFrom(
this.http.get<AuthorizationResponse>('/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;
}
}
}
}
Expand All @@ -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);
}
}

0 comments on commit 2caf01f

Please sign in to comment.