Add route parameter handling for appeal paths and enhance AuthGuard to support login via code query parameter

This commit is contained in:
akastijn 2025-11-08 17:42:29 +01:00
parent e8f952e7e2
commit e83d109012
2 changed files with 28 additions and 4 deletions

View File

@ -118,18 +118,27 @@ export const routes: Routes = [
path: 'forms', path: 'forms',
loadComponent: () => import('./pages/forms/forms.component').then(m => m.FormsComponent) loadComponent: () => import('./pages/forms/forms.component').then(m => m.FormsComponent)
}, },
{
path: 'appeal/:code',
redirectTo: 'forms/appeal/:code',
pathMatch: 'full'
},
{ {
path: 'appeal', path: 'appeal',
redirectTo: 'forms/appeal', redirectTo: 'forms/appeal',
pathMatch: 'full' pathMatch: 'full'
}, },
{
path: 'forms/appeal/:code',
loadComponent: () => import('./pages/forms/appeal/appeal.component').then(m => m.AppealComponent),
canActivate: [AuthGuard],
data: {requiredAuthorizations: ['SCOPE_user']}
},
{ {
path: 'forms/appeal', path: 'forms/appeal',
loadComponent: () => import('./pages/forms/appeal/appeal.component').then(m => m.AppealComponent), loadComponent: () => import('./pages/forms/appeal/appeal.component').then(m => m.AppealComponent),
canActivate: [AuthGuard], canActivate: [AuthGuard],
data: { data: {requiredAuthorizations: ['SCOPE_user']}
requiredAuthorizations: ['SCOPE_user']
}
}, },
{ {
path: 'forms/sent', path: 'forms/sent',

View File

@ -1,9 +1,10 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router'; import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
import {map, Observable} from 'rxjs'; import {from, isObservable, map, Observable, of, switchMap} from 'rxjs';
import {AuthService} from '@services/auth.service'; import {AuthService} from '@services/auth.service';
import {MatDialog} from '@angular/material/dialog'; import {MatDialog} from '@angular/material/dialog';
import {LoginDialogComponent} from '@shared-components/login/login.component'; import {LoginDialogComponent} from '@shared-components/login/login.component';
import {catchError} from 'rxjs/operators';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -21,6 +22,20 @@ export class AuthGuard implements CanActivate {
route: ActivatedRouteSnapshot, route: ActivatedRouteSnapshot,
state: RouterStateSnapshot state: RouterStateSnapshot
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree { ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
const code = route.paramMap.get('code');
if (code) {
return this.authService.login(code).pipe(
switchMap(() => {
const result = this.canActivateInternal(route, state);
return isObservable(result) ? result : result instanceof Promise ? from(result) : of(result);
}),
catchError(() => of(this.router.createUrlTree(['/'])))
);
}
return this.canActivateInternal(route, state);
}
private canActivateInternal(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
if (!this.authService.isAuthenticated$()) { if (!this.authService.isAuthenticated$()) {
this.router.createUrlTree(['/']); this.router.createUrlTree(['/']);
const dialogRef = this.dialog.open(LoginDialogComponent, { const dialogRef = this.dialog.open(LoginDialogComponent, {