frontend: enhance navigation service to manage leave redirect URL with session storage
This commit is contained in:
parent
aba5269b54
commit
fe29e0dba6
@ -9,6 +9,10 @@ export const extractRoomQueryParamsGuard: CanActivateFn = (route: ActivatedRoute
|
|||||||
const participantService = inject(ParticipantTokenService);
|
const participantService = inject(ParticipantTokenService);
|
||||||
const { roomId, participantName, secret, leaveRedirectUrl, showOnlyRecordings } = extractParams(route);
|
const { roomId, participantName, secret, leaveRedirectUrl, showOnlyRecordings } = extractParams(route);
|
||||||
|
|
||||||
|
if (isValidUrl(leaveRedirectUrl)) {
|
||||||
|
navigationService.setLeaveRedirectUrl(leaveRedirectUrl);
|
||||||
|
}
|
||||||
|
|
||||||
if (!secret) {
|
if (!secret) {
|
||||||
// If no secret is provided, redirect to the error page
|
// If no secret is provided, redirect to the error page
|
||||||
return navigationService.redirectToErrorPage(ErrorReason.MISSING_ROOM_SECRET);
|
return navigationService.redirectToErrorPage(ErrorReason.MISSING_ROOM_SECRET);
|
||||||
@ -21,10 +25,6 @@ export const extractRoomQueryParamsGuard: CanActivateFn = (route: ActivatedRoute
|
|||||||
participantService.setParticipantName(participantName);
|
participantService.setParticipantName(participantName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isValidUrl(leaveRedirectUrl)) {
|
|
||||||
navigationService.setLeaveRedirectUrl(leaveRedirectUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (showOnlyRecordings === 'true') {
|
if (showOnlyRecordings === 'true') {
|
||||||
// Redirect to the room recordings page
|
// Redirect to the room recordings page
|
||||||
return navigationService.createRedirectionTo(`room/${roomId}/recordings`, { secret });
|
return navigationService.createRedirectionTo(`room/${roomId}/recordings`, { secret });
|
||||||
|
|||||||
@ -2,35 +2,60 @@ import { Location } from '@angular/common';
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { Params, Router, UrlTree } from '@angular/router';
|
import { Params, Router, UrlTree } from '@angular/router';
|
||||||
import { ErrorReason } from '@lib/models';
|
import { ErrorReason } from '@lib/models';
|
||||||
|
import { SessionStorageService } from '@lib/services';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class NavigationService {
|
export class NavigationService {
|
||||||
protected leaveRedirectUrl: string = '';
|
protected leaveRedirectUrl?: string;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private router: Router,
|
private router: Router,
|
||||||
private location: Location
|
private location: Location,
|
||||||
|
private sessionStorageService: SessionStorageService
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
setLeaveRedirectUrl(leaveRedirectUrl: string): void {
|
setLeaveRedirectUrl(leaveRedirectUrl: string): void {
|
||||||
this.leaveRedirectUrl = leaveRedirectUrl;
|
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;
|
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
|
* Redirects to internal or external URLs
|
||||||
*
|
*
|
||||||
* @param url - The URL to redirect to
|
* @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> {
|
async redirectTo(url: string): Promise<void> {
|
||||||
if (isExternal) {
|
const isExternalURL = /^https?:\/\//.test(url);
|
||||||
|
if (isExternalURL) {
|
||||||
console.log('Redirecting to external URL:', url);
|
console.log('Redirecting to external URL:', url);
|
||||||
window.location.href = url;
|
window.location.href = url;
|
||||||
} else {
|
} 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.
|
* Creates a UrlTree for the error page with specific reason and optionally navigates to it.
|
||||||
*
|
*
|
||||||
@ -88,35 +124,6 @@ export class NavigationService {
|
|||||||
return urlTree;
|
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
|
* Checks if the current URL contains a specific route
|
||||||
*
|
*
|
||||||
|
|||||||
@ -39,6 +39,24 @@ export class SessionStorageService {
|
|||||||
this.remove(`moderator_secret_${roomId}`);
|
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.
|
* Clears all data stored in sessionStorage.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user