import {Injectable} from '@angular/core'; import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router'; import {map, Observable} from 'rxjs'; import {AuthService} from '@services/auth.service'; import {MatDialog} from '@angular/material/dialog'; import {LoginDialogComponent} from '@shared-components/login/login.component'; @Injectable({ providedIn: 'root' }) export class AuthGuard implements CanActivate { constructor( private authService: AuthService, private router: Router, private dialog: MatDialog ) { } canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable | Promise | boolean | UrlTree { if (!this.authService.checkAuthStatus()) { this.router.createUrlTree(['/']); const dialogRef = this.dialog.open(LoginDialogComponent, { width: '400px', }) return dialogRef.afterClosed().pipe( map(result => { if (result) { return this.router.createUrlTree([state.url]); } return this.router.createUrlTree(['/']); }) ); } const requiredAuthorizations = route.data['requiredAuthorizations'] as string[]; if (!requiredAuthorizations || requiredAuthorizations.length === 0) { return true; } const userAuthorizations = this.authService.getUserAuthorizations(); const hasAccess = requiredAuthorizations.some(auth => userAuthorizations.includes(auth)); if (!hasAccess) { return this.router.createUrlTree(['/']); } return true; } }