import { Injectable, inject } from'@angular/core';import { Router, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild } from'@angular/router';import { AuthenticationService } from'../../service/authentication.service';@Injectable({ providedIn:'root' })exportclassAuthGuardChildimplementsCanActivateChild {private router =inject(Router);private authenticationService =inject(AuthenticationService);/** * Determines whether a child route can be activated based on user authentication and authorization. * * @param route - The activated route snapshot that contains the route configuration and parameters. * @param state - The router state snapshot containing the current state. * @returns A boolean indicating whether the route can be activated. Redirects to an appropriate page if not. * * If the user is logged in and their role is authorized for the route, it returns true. * If the user is logged in but not authorized, redirects to the unauthorized page and returns false. * If the user is not logged in, redirects to the login page with the return URL and returns false. */canActivateChild(route:ActivatedRouteSnapshot, state:RouterStateSnapshot) {constcurrentUser=this.authenticationService.currentUserValue;if (currentUser &&this.authenticationService.isLoggedIn()) {const { roles } =route.data;if (roles &&!roles.includes(currentUser.user.role)) {// User not authorized, redirect to unauthorized pagethis.router.navigate(['/unauthorized']);returnfalse; }// User is logged in and authorized for child routesreturntrue; }// User not logged in, redirect to login pagethis.router.navigate(['/login'], { queryParams: { returnUrl:state.url } });returnfalse; }}