frontend: enhance navigation service to manage leave redirect URL with session storage

This commit is contained in:
juancarmore 2025-07-19 00:22:07 +02:00
parent aba5269b54
commit fe29e0dba6
3 changed files with 65 additions and 40 deletions

View File

@ -9,6 +9,10 @@ export const extractRoomQueryParamsGuard: CanActivateFn = (route: ActivatedRoute
const participantService = inject(ParticipantTokenService);
const { roomId, participantName, secret, leaveRedirectUrl, showOnlyRecordings } = extractParams(route);
if (isValidUrl(leaveRedirectUrl)) {
navigationService.setLeaveRedirectUrl(leaveRedirectUrl);
}
if (!secret) {
// If no secret is provided, redirect to the error page
return navigationService.redirectToErrorPage(ErrorReason.MISSING_ROOM_SECRET);
@ -21,10 +25,6 @@ export const extractRoomQueryParamsGuard: CanActivateFn = (route: ActivatedRoute
participantService.setParticipantName(participantName);
}
if (isValidUrl(leaveRedirectUrl)) {
navigationService.setLeaveRedirectUrl(leaveRedirectUrl);
}
if (showOnlyRecordings === 'true') {
// Redirect to the room recordings page
return navigationService.createRedirectionTo(`room/${roomId}/recordings`, { secret });

View File

@ -2,35 +2,60 @@ import { Location } from '@angular/common';
import { Injectable } from '@angular/core';
import { Params, Router, UrlTree } from '@angular/router';
import { ErrorReason } from '@lib/models';
import { SessionStorageService } from '@lib/services';
@Injectable({
providedIn: 'root'
})
export class NavigationService {
protected leaveRedirectUrl: string = '';
protected leaveRedirectUrl?: string;
constructor(
private router: Router,
private location: Location
private location: Location,
private sessionStorageService: SessionStorageService
) {}
setLeaveRedirectUrl(leaveRedirectUrl: string): void {
this.leaveRedirectUrl = leaveRedirectUrl;
this.sessionStorageService.setRedirectUrl(leaveRedirectUrl);
}
getLeaveRedirectURL(): string {
getLeaveRedirectURL(): string | undefined {
const storedRedirectUrl = this.sessionStorageService.getRedirectUrl();
if (!this.leaveRedirectUrl && storedRedirectUrl) {
this.leaveRedirectUrl = storedRedirectUrl;
}
return this.leaveRedirectUrl;
}
/**
* Navigates to a specific route
*
* @param route - The route to navigate to
* @param queryParams - Optional query parameters to include in the navigation
* @param replaceUrl - If true, replaces the current URL in the browser history
*/
async navigateTo(route: string, queryParams?: Params, replaceUrl: boolean = false): Promise<void> {
try {
await this.router.navigate([route], {
queryParams,
replaceUrl
});
} catch (error) {
console.error('Error navigating to route:', error);
}
}
/**
* Redirects to internal or external URLs
*
* @param url - The URL to redirect to
* @param isExternal - If true, treats the URL as an external link; otherwise,
* treats it as an internal route
*/
async redirectTo(url: string, isExternal = false): Promise<void> {
if (isExternal) {
async redirectTo(url: string): Promise<void> {
const isExternalURL = /^https?:\/\//.test(url);
if (isExternalURL) {
console.log('Redirecting to external URL:', url);
window.location.href = url;
} else {
@ -45,6 +70,17 @@ export class NavigationService {
}
}
/**
* Creates a URL tree for redirecting to a specific route
*
* @param route - The route to redirect to
* @param queryParams - Optional query parameters to include in the URL
* @returns A UrlTree representing the redirection
*/
createRedirectionTo(route: string, queryParams?: Params): UrlTree {
return this.router.createUrlTree([route], { queryParams });
}
/**
* Creates a UrlTree for the error page with specific reason and optionally navigates to it.
*
@ -88,35 +124,6 @@ export class NavigationService {
return urlTree;
}
/**
* Creates a URL tree for redirecting to a specific route
*
* @param route - The route to redirect to
* @param queryParams - Optional query parameters to include in the URL
* @returns A UrlTree representing the redirection
*/
createRedirectionTo(route: string, queryParams?: Params): UrlTree {
return this.router.createUrlTree([route], { queryParams });
}
/**
* Navigates to a specific route
*
* @param route - The route to navigate to
* @param queryParams - Optional query parameters to include in the navigation
* @param replaceUrl - If true, replaces the current URL in the browser history
*/
async navigateTo(route: string, queryParams?: Params, replaceUrl: boolean = false): Promise<void> {
try {
await this.router.navigate([route], {
queryParams,
replaceUrl
});
} catch (error) {
console.error('Error navigating to route:', error);
}
}
/**
* Checks if the current URL contains a specific route
*

View File

@ -39,6 +39,24 @@ export class SessionStorageService {
this.remove(`moderator_secret_${roomId}`);
}
/**
* Stores a redirect URL to be used after leaving OpenVidu Meet.
*
* @param redirectUrl The URL to redirect to.
*/
public setRedirectUrl(redirectUrl: string): void {
this.set('redirect_url', redirectUrl);
}
/**
* Retrieves the redirect URL stored in sessionStorage.
*
* @returns The redirect URL or null if not found.
*/
public getRedirectUrl(): string | null {
return this.get<string>('redirect_url') ?? null;
}
/**
* Clears all data stored in sessionStorage.
*/