add initial Angular components, services, and routing setup
This commit is contained in:
17
client/src/app/guards/admin-only/admin-only.guard.spec.ts
Normal file
17
client/src/app/guards/admin-only/admin-only.guard.spec.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { CanActivateFn } from '@angular/router';
|
||||
|
||||
import { adminOnlyGuard } from './admin-only.guard';
|
||||
|
||||
describe('adminOnlyGuard', () => {
|
||||
const executeGuard: CanActivateFn = (...guardParameters) =>
|
||||
TestBed.runInInjectionContext(() => adminOnlyGuard(...guardParameters));
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(executeGuard).toBeTruthy();
|
||||
});
|
||||
});
|
||||
27
client/src/app/guards/admin-only/admin-only.guard.ts
Normal file
27
client/src/app/guards/admin-only/admin-only.guard.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { inject } from '@angular/core';
|
||||
import { CanActivateFn, CanMatchFn, Router, UrlTree, ActivatedRouteSnapshot, Route } from '@angular/router';
|
||||
import { AuthService } from '../../services/auth/auth.service';
|
||||
|
||||
function requireAdmin(url?: string): boolean | UrlTree {
|
||||
const authService: AuthService = inject(AuthService);
|
||||
const router: Router = inject(Router);
|
||||
|
||||
// 1) pas connecté -> envoie vers /login (avec redirect)
|
||||
if (!authService.isLoggedIn()) {
|
||||
return router.createUrlTree(['/login'], { queryParams: { redirect: url ?? router.url } });
|
||||
}
|
||||
|
||||
// 2) connecté mais pas ADMIN -> redirige vers /home (ou /403 si tu crées une page)
|
||||
if (!authService.hasRole('Administrator')) {
|
||||
return router.parseUrl('/home');
|
||||
}
|
||||
|
||||
// 3) ok
|
||||
return true;
|
||||
}
|
||||
|
||||
export const adminOnlyCanActivate: CanActivateFn = (route: ActivatedRouteSnapshot): boolean | UrlTree =>
|
||||
requireAdmin(route?.url?.map(u => u.path).join('/') ?? '/admin');
|
||||
|
||||
export const adminOnlyCanMatch: CanMatchFn = (route: Route): boolean | UrlTree =>
|
||||
requireAdmin(route?.path ?? '/admin');
|
||||
17
client/src/app/guards/auth-only/auth-only.guard.spec.ts
Normal file
17
client/src/app/guards/auth-only/auth-only.guard.spec.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { CanMatchFn } from '@angular/router';
|
||||
|
||||
import { authOnlyGuard } from './auth-only.guard';
|
||||
|
||||
describe('authOnlyGuard', () => {
|
||||
const executeGuard: CanMatchFn = (...guardParameters) =>
|
||||
TestBed.runInInjectionContext(() => authOnlyGuard(...guardParameters));
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(executeGuard).toBeTruthy();
|
||||
});
|
||||
});
|
||||
21
client/src/app/guards/auth-only/auth-only.guard.ts
Normal file
21
client/src/app/guards/auth-only/auth-only.guard.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { inject } from '@angular/core';
|
||||
import { CanActivateFn, CanMatchFn, Router, UrlTree, ActivatedRouteSnapshot, Route } from '@angular/router';
|
||||
import { AuthService } from '../../services/auth/auth.service';
|
||||
|
||||
function requireAuth(url?: string): boolean | UrlTree {
|
||||
const authService = inject(AuthService);
|
||||
const router = inject(Router);
|
||||
|
||||
if (authService.isLoggedIn()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// redirige vers /login avec un param "redirect"
|
||||
return router.createUrlTree(['/login'], { queryParams: { redirect: url ?? router.url } });
|
||||
}
|
||||
|
||||
export const authOnlyCanActivate: CanActivateFn = (route: ActivatedRouteSnapshot): boolean | UrlTree =>
|
||||
requireAuth(route?.url?.map(urlSegment => urlSegment.path).join('/') ?? '/login');
|
||||
|
||||
export const authOnlyCanMatch: CanMatchFn = (route: Route): boolean | UrlTree =>
|
||||
requireAuth(route?.path ?? '/login');
|
||||
17
client/src/app/guards/guest-only/guest-only.guard.spec.ts
Normal file
17
client/src/app/guards/guest-only/guest-only.guard.spec.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { CanMatchFn } from '@angular/router';
|
||||
|
||||
import { guestOnlyGuard } from './guest-only.guard';
|
||||
|
||||
describe('guestOnlyGuard', () => {
|
||||
const executeGuard: CanMatchFn = (...guardParameters) =>
|
||||
TestBed.runInInjectionContext(() => guestOnlyGuard(...guardParameters));
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(executeGuard).toBeTruthy();
|
||||
});
|
||||
});
|
||||
13
client/src/app/guards/guest-only/guest-only.guard.ts
Normal file
13
client/src/app/guards/guest-only/guest-only.guard.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { inject } from '@angular/core';
|
||||
import { Router, UrlTree, CanActivateFn, CanMatchFn } from '@angular/router';
|
||||
import { AuthService } from '../../services/auth/auth.service';
|
||||
|
||||
function redirectIfLoggedIn(): boolean | UrlTree {
|
||||
const authService = inject(AuthService);
|
||||
const router = inject(Router);
|
||||
// Si déjà connecté -> redirige vers /home
|
||||
return authService.isLoggedIn() ? router.parseUrl('/home') : true;
|
||||
}
|
||||
|
||||
export const guestOnlyCanMatch: CanMatchFn = () => redirectIfLoggedIn();
|
||||
export const guestOnlyCanActivate: CanActivateFn = () => redirectIfLoggedIn();
|
||||
Reference in New Issue
Block a user