Prompt login dialog when user is unauthenticated during auth guard check.

This commit is contained in:
akastijn 2025-10-12 21:40:54 +02:00
parent 5a4df2572d
commit b3999b3389

View File

@ -1,7 +1,9 @@
import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
import {Observable} from 'rxjs';
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'
@ -10,7 +12,8 @@ export class AuthGuard implements CanActivate {
constructor(
private authService: AuthService,
private router: Router
private router: Router,
private dialog: MatDialog
) {
}
@ -19,7 +22,16 @@ export class AuthGuard implements CanActivate {
state: RouterStateSnapshot
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
if (!this.authService.checkAuthStatus()) {
return this.router.createUrlTree(['/']);
this.router.createUrlTree(['/']);
const dialogRef = this.dialog.open(LoginDialogComponent);
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[];