frontend: reorder guards in base routes to fix bug when redirecting to error page

This commit is contained in:
juancarmore 2025-08-13 23:09:03 +02:00
parent 29019f82ff
commit 5f71b0c28a
2 changed files with 39 additions and 39 deletions

View File

@ -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);
}
}
};

View File

@ -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
)
]
},