37 lines
1020 B
TypeScript

import { inject } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivateFn } from '@angular/router';
import { ContextService } from '../services';
export const extractQueryParamsGuard: CanActivateFn = (route: ActivatedRouteSnapshot) => {
const contextService = inject(ContextService);
const { roomId, participantName, secret, leaveRedirectUrl } = extractParams(route);
if (isValidUrl(leaveRedirectUrl)) {
contextService.setLeaveRedirectUrl(leaveRedirectUrl);
}
contextService.setRoomId(roomId);
contextService.setParticipantName(participantName);
contextService.setSecret(secret);
return true;
};
const extractParams = (route: ActivatedRouteSnapshot) => ({
roomId: route.params['room-id'],
participantName: route.queryParams['participant-name'],
secret: route.queryParams['secret'],
leaveRedirectUrl: route.queryParams['leave-redirect-url']
});
const isValidUrl = (url: string) => {
if (!url) return false;
try {
new URL(url);
return true;
} catch (error) {
return false;
}
};