connexion avec le back
This commit is contained in:
parent
91e4bba793
commit
4620f0dff9
16 changed files with 178 additions and 110 deletions
16
frontend/src/app/common/guards/admin/admin.guard.spec.ts
Normal file
16
frontend/src/app/common/guards/admin/admin.guard.spec.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { AdminGuard } from './admin.guard';
|
||||
|
||||
describe('AdminGuard', () => {
|
||||
let guard: AdminGuard;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
guard = TestBed.inject(AdminGuard);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(guard).toBeTruthy();
|
||||
});
|
||||
});
|
||||
31
frontend/src/app/common/guards/admin/admin.guard.ts
Normal file
31
frontend/src/app/common/guards/admin/admin.guard.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
|
||||
import { Observable } from 'rxjs';
|
||||
import {ProfilService} from "../../services/profil/profil.service";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AdminGuard implements CanActivate
|
||||
{
|
||||
|
||||
constructor(private profilService: ProfilService, private router: Router) {}
|
||||
|
||||
|
||||
canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
|
||||
{
|
||||
if(this.profilService.getId() === -1) // si non connecté
|
||||
{
|
||||
this.router.navigateByUrl("/login");
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
if(this.profilService.isAdmin()) return true;
|
||||
else {
|
||||
this.router.navigateByUrl("/login");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
16
frontend/src/app/common/guards/user/user.guard.spec.ts
Normal file
16
frontend/src/app/common/guards/user/user.guard.spec.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { UserGuard } from './user.guard';
|
||||
|
||||
describe('UserGuard', () => {
|
||||
let guard: UserGuard;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
guard = TestBed.inject(UserGuard);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(guard).toBeTruthy();
|
||||
});
|
||||
});
|
||||
31
frontend/src/app/common/guards/user/user.guard.ts
Normal file
31
frontend/src/app/common/guards/user/user.guard.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
|
||||
import { Observable } from 'rxjs';
|
||||
import {ProfilService} from "../../services/profil/profil.service";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UserGuard implements CanActivate
|
||||
{
|
||||
|
||||
constructor(private profilService: ProfilService, private router: Router) {}
|
||||
|
||||
|
||||
canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
|
||||
{
|
||||
if(this.profilService.getId() === -1) // si non connecté
|
||||
{
|
||||
this.router.navigateByUrl("/login");
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
if(!this.profilService.isAdmin()) return true;
|
||||
else {
|
||||
this.router.navigateByUrl("/login");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue