All files / app/guards employee.guard.ts

100% Statements 15/15
100% Branches 7/7
100% Functions 1/1
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31        1x 8x 8x   8x 1x 1x     7x 7x 1x 1x       6x 6x 2x       4x 4x      
import { inject } from '@angular/core';
import { Router, CanActivateFn } from '@angular/router';
import { AuthService } from '../services/auth.service';
 
export const employeeGuard: CanActivateFn = (route, state) => {
  const authService = inject(AuthService);
  const router = inject(Router);
 
  if (!authService.isAuthenticated()) {
    router.navigate(['/login']);
    return false;
  }
 
  const currentUser = authService.getCurrentUserSync();
  if (!currentUser) {
    router.navigate(['/login']);
    return false;
  }
 
  // Vérifier si l'utilisateur a le rôle EMPLOYEE ou ADMIN
  const role = currentUser.role?.name || '';
  if (role === 'ROLE_EMPLOYEE' || role === 'ROLE_ADMIN') {
    return true;
  }
 
  // Rediriger vers l'accueil si pas le bon rôle
  router.navigate(['/']);
  return false;
};