ajout de la guard
This commit is contained in:
parent
b9c3e7d0f3
commit
c55af87a23
5 changed files with 34853 additions and 4 deletions
34806
package-lock.json
generated
Normal file
34806
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -33,6 +33,7 @@ export class NavbarAdminComponent
|
|||
onDeconnexionCallback(retour: any): void
|
||||
{
|
||||
if(retour.status !== "success") console.log(retour);
|
||||
this.profilService.setId("");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import {PageRegisterComponent} from "./beforeConnexion/register/page-register/pa
|
|||
import {PageAdListAdminComponent} from "./admin/adList/page-ad-list-admin/page-ad-list-admin.component";
|
||||
import {PageProfilAdminComponent} from "./admin/myProfil/page-profil-admin/page-profil-admin.component";
|
||||
import {PageUserListComponent} from "./admin/userList/page-user-list/page-user-list.component";
|
||||
import {MyGuardGuard} from "./utils/my-guard/my-guard.guard";
|
||||
|
||||
const routes: Routes = [
|
||||
|
||||
|
|
@ -14,10 +15,10 @@ const routes: Routes = [
|
|||
{ path: 'register', component: PageRegisterComponent },
|
||||
|
||||
// Admin
|
||||
{ path: 'admin', component: PageUserListComponent },
|
||||
{ path: 'admin/userList', component: PageUserListComponent },
|
||||
{ path: 'admin/adList', component: PageAdListAdminComponent },
|
||||
{ path: 'admin/myProfil', component: PageProfilAdminComponent },
|
||||
{ path: 'admin', component: PageUserListComponent, canActivate: [MyGuardGuard] },
|
||||
{ path: 'admin/userList', component: PageUserListComponent, canActivate: [MyGuardGuard] },
|
||||
{ path: 'admin/adList', component: PageAdListAdminComponent, canActivate: [MyGuardGuard] },
|
||||
{ path: 'admin/myProfil', component: PageProfilAdminComponent, canActivate: [MyGuardGuard] },
|
||||
];
|
||||
|
||||
@NgModule({
|
||||
|
|
|
|||
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