18 lines
579 B
TypeScript
18 lines
579 B
TypeScript
import {inject} from '@angular/core';
|
|
import {CanActivateFn, Router} from '@angular/router';
|
|
import {AuthService} from '../services/auth';
|
|
|
|
export function roleGuard(allowedRoles: string[]): CanActivateFn {
|
|
return () => {
|
|
const authService = inject(AuthService);
|
|
const router = inject(Router);
|
|
|
|
const role = authService.currentUserRole();
|
|
if (role && allowedRoles.includes(role)) {
|
|
return true;
|
|
}
|
|
|
|
console.warn(`Zugriff verweigert: benötigt eine der Rollen [${allowedRoles.join(', ')}].`);
|
|
return router.createUrlTree(['/dashboard']);
|
|
};
|
|
}
|