frontend: Enhance token refresh logic in HTTP interceptor for admin and participant routes

This commit is contained in:
juancarmore 2025-03-14 01:38:45 +01:00
parent 314b9fae21
commit fe01ddb17e
2 changed files with 48 additions and 14 deletions

View File

@ -1,10 +1,15 @@
import { HttpErrorResponse, HttpHandlerFn, HttpInterceptorFn, HttpRequest } from '@angular/common/http';
import { catchError, switchMap } from 'rxjs';
import { AuthService } from '../services/auth/auth.service';
import { catchError, from, switchMap } from 'rxjs';
import { AuthService, ContextService, HttpService, SessionStorageService } from '../services';
import { inject } from '@angular/core';
import { Router } from '@angular/router';
export const httpInterceptor: HttpInterceptorFn = (req: HttpRequest<unknown>, next: HttpHandlerFn) => {
const router: Router = inject(Router);
const authService: AuthService = inject(AuthService);
const contextService = inject(ContextService);
const sessionStorageService = inject(SessionStorageService);
const httpService: HttpService = inject(HttpService);
req = req.clone({
withCredentials: true
@ -14,18 +19,43 @@ export const httpInterceptor: HttpInterceptorFn = (req: HttpRequest<unknown>, ne
catchError((error: HttpErrorResponse) => {
if (error.status === 401) {
// Expired access token
console.log('Refreshing token...');
return authService.adminRefresh().pipe(
switchMap(() => {
console.log('Token refreshed');
return next(req);
}),
catchError(() => {
console.log('Error refreshing token. Logging out...');
authService.adminLogout();
throw error;
})
);
// Get current URL to determine if it's an admin or participant route
const url = router.getCurrentNavigation()?.finalUrl?.toString() || router.url;
if (url.startsWith('/console')) {
if (!url.includes('login')) {
console.log('Refreshing admin token...');
return authService.adminRefresh().pipe(
switchMap(() => {
console.log('Admin token refreshed');
return next(req);
}),
catchError(() => {
console.error('Error refreshing admin token. Logging out...');
authService.adminLogout();
throw error;
})
);
}
} else if (url.startsWith('/room')) {
console.log('Refreshing participant token...');
const roomName = contextService.getRoomName();
const participantName = contextService.getParticipantName();
const storedSecret = sessionStorageService.getModeratorSecret(roomName);
const secret = storedSecret || contextService.getSecret();
return from(httpService.refreshParticipantToken({ roomName, participantName, secret })).pipe(
switchMap((data) => {
console.log('Participant token refreshed');
contextService.setToken(data.token);
return next(req);
}),
catchError(() => {
console.error('Error refreshing participant token');
throw error;
})
);
}
}
throw error;

View File

@ -43,6 +43,10 @@ export class HttpService {
return this.postRequest(`${this.pathPrefix}/participants/token`, tokenOptions);
}
refreshParticipantToken(tokenOptions: TokenOptions): Promise<{ token: string }> {
return this.postRequest(`${this.pathPrefix}/participants/token/refresh`, tokenOptions);
}
/**
* Retrieves the global preferences.
*