From fe01ddb17e336d45033272d8d3ddcb1849fcefcc Mon Sep 17 00:00:00 2001 From: juancarmore Date: Fri, 14 Mar 2025 01:38:45 +0100 Subject: [PATCH] frontend: Enhance token refresh logic in HTTP interceptor for admin and participant routes --- .../src/lib/interceptors/http.interceptor.ts | 58 ++++++++++++++----- .../src/lib/services/http/http.service.ts | 4 ++ 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/frontend/projects/shared-meet-components/src/lib/interceptors/http.interceptor.ts b/frontend/projects/shared-meet-components/src/lib/interceptors/http.interceptor.ts index b37e461..7380f3c 100644 --- a/frontend/projects/shared-meet-components/src/lib/interceptors/http.interceptor.ts +++ b/frontend/projects/shared-meet-components/src/lib/interceptors/http.interceptor.ts @@ -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, 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, 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; diff --git a/frontend/projects/shared-meet-components/src/lib/services/http/http.service.ts b/frontend/projects/shared-meet-components/src/lib/services/http/http.service.ts index 326e114..48d6d99 100644 --- a/frontend/projects/shared-meet-components/src/lib/services/http/http.service.ts +++ b/frontend/projects/shared-meet-components/src/lib/services/http/http.service.ts @@ -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. *