ajout de la guard
This commit is contained in:
parent
b9c3e7d0f3
commit
c55af87a23
5 changed files with 34853 additions and 4 deletions
16
src/app/utils/my-guard/my-guard.guard.spec.ts
Normal file
16
src/app/utils/my-guard/my-guard.guard.spec.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { MyGuardGuard } from './my-guard.guard';
|
||||
|
||||
describe('MyGuardGuard', () => {
|
||||
let guard: MyGuardGuard;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
guard = TestBed.inject(MyGuardGuard);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(guard).toBeTruthy();
|
||||
});
|
||||
});
|
||||
25
src/app/utils/my-guard/my-guard.guard.ts
Normal file
25
src/app/utils/my-guard/my-guard.guard.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
|
||||
import { Observable } from 'rxjs';
|
||||
import {ProfilService} from "../profil/profil.service";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class MyGuardGuard 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() !== "") return true;
|
||||
else {
|
||||
this.router.navigateByUrl("login");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in a new issue