diff --git a/frontend/projects/shared-meet-components/src/lib/guards/validate-access.guard.ts b/frontend/projects/shared-meet-components/src/lib/guards/validate-access.guard.ts index f9ade41..ba50629 100644 --- a/frontend/projects/shared-meet-components/src/lib/guards/validate-access.guard.ts +++ b/frontend/projects/shared-meet-components/src/lib/guards/validate-access.guard.ts @@ -3,6 +3,41 @@ import { ActivatedRouteSnapshot, CanActivateFn, RouterStateSnapshot } from '@ang import { ErrorReason } from '@lib/models'; import { NavigationService, ParticipantService, RecordingService, RoomService } from '@lib/services'; +/** + * Guard to validate access to a room by generating a participant token. + */ +export const validateRoomAccessGuard: CanActivateFn = async ( + _route: ActivatedRouteSnapshot, + _state: RouterStateSnapshot +) => { + const roomService = inject(RoomService); + const participantTokenService = inject(ParticipantService); + const navigationService = inject(NavigationService); + + const roomId = roomService.getRoomId(); + const secret = roomService.getRoomSecret(); + + try { + await participantTokenService.generateToken({ + roomId, + secret + }); + return true; + } catch (error: any) { + console.error('Error generating participant token:', error); + switch (error.status) { + case 400: + // Invalid secret + return navigationService.redirectToErrorPage(ErrorReason.INVALID_ROOM_SECRET); + case 404: + // Room not found + return navigationService.redirectToErrorPage(ErrorReason.INVALID_ROOM); + default: + return navigationService.redirectToErrorPage(ErrorReason.INTERNAL_ERROR); + } + } +}; + /** * Guard to validate the access to recordings of a room by generating a recording token. */ @@ -44,38 +79,3 @@ export const validateRecordingAccessGuard: CanActivateFn = async ( } } }; - -/** - * Guard to validate access to a room by generating a participant token. - */ -export const validateRoomAccessGuard: CanActivateFn = async ( - _route: ActivatedRouteSnapshot, - _state: RouterStateSnapshot -) => { - const roomService = inject(RoomService); - const participantTokenService = inject(ParticipantService); - const navigationService = inject(NavigationService); - - const roomId = roomService.getRoomId(); - const secret = roomService.getRoomSecret(); - - try { - await participantTokenService.generateToken({ - roomId, - secret - }); - return true; - } catch (error: any) { - console.error('Error generating participant token:', error); - switch (error.status) { - case 400: - // Invalid secret - return navigationService.redirectToErrorPage(ErrorReason.INVALID_ROOM_SECRET); - case 404: - // Room not found - return navigationService.redirectToErrorPage(ErrorReason.INVALID_ROOM); - default: - return navigationService.redirectToErrorPage(ErrorReason.INTERNAL_ERROR); - } - } -}; diff --git a/frontend/projects/shared-meet-components/src/lib/routes/base-routes.ts b/frontend/projects/shared-meet-components/src/lib/routes/base-routes.ts index 62742b3..9213120 100644 --- a/frontend/projects/shared-meet-components/src/lib/routes/base-routes.ts +++ b/frontend/projects/shared-meet-components/src/lib/routes/base-routes.ts @@ -39,9 +39,9 @@ export const baseRoutes: Routes = [ canActivate: [ runGuardsSerially( extractRoomQueryParamsGuard, - removeRoomSecretGuard, checkParticipantRoleAndAuthGuard, - validateRoomAccessGuard + validateRoomAccessGuard, + removeRoomSecretGuard ) ] }, @@ -51,9 +51,9 @@ export const baseRoutes: Routes = [ canActivate: [ runGuardsSerially( extractRecordingQueryParamsGuard, - removeRoomSecretGuard, checkParticipantRoleAndAuthGuard, - validateRecordingAccessGuard + validateRecordingAccessGuard, + removeRoomSecretGuard ) ] },