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

View File

@ -43,6 +43,10 @@ export class HttpService {
return this.postRequest(`${this.pathPrefix}/participants/token`, tokenOptions); 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. * Retrieves the global preferences.
* *