diff --git a/src/app/advertiser/utils/navbar-advertiser/navbar-advertiser.component.ts b/src/app/advertiser/utils/navbar-advertiser/navbar-advertiser.component.ts index c65b5fe..a2cf5d4 100644 --- a/src/app/advertiser/utils/navbar-advertiser/navbar-advertiser.component.ts +++ b/src/app/advertiser/utils/navbar-advertiser/navbar-advertiser.component.ts @@ -36,6 +36,7 @@ export class NavbarAdvertiserComponent onDeconnexionCallback(retour: any): void { if(retour.status !== "success") console.log(retour); + this.profilService.setRole(""); } } diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index d1888ec..bb73ad4 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -10,6 +10,8 @@ import {PageHistoryUserComponent} from "./user/history/page-history-user/page-hi import {PageAdListAdvertiserComponent} from "./advertiser/adList/page-ad-list-advertiser/page-ad-list-advertiser.component"; import {PagesPopularityComponent} from "./advertiser/pages-popularity/pages-popularity.component"; import {PageProfilAdvertiserComponent} from "./advertiser/myProfil/page-profil-advertiser/page-profil-advertiser.component"; +import {UserGuard} from "./utils/guards/user/user.guard"; +import {AdvertiserGuard} from "./utils/guards/advertiser/advertiser.guard"; const routes: Routes = [ @@ -19,19 +21,19 @@ const routes: Routes = [ { path: 'register', component: PageRegisterComponent }, // User - { path: 'user', component: PageSearchComponent }, - { path: 'user/search', component: PageSearchComponent }, - { path: 'user/myPlaylists', component: PageMyPlaylistsComponent }, - { path: 'user/history', component: PageHistoryUserComponent }, - { path: 'user/myProfil', component: PageProfilUserComponent }, - { path: 'user/watching', component: PageWatchingVideoComponent }, + { path: 'user', component: PageSearchComponent, canActivate: [UserGuard] }, + { path: 'user/search', component: PageSearchComponent, canActivate: [UserGuard] }, + { path: 'user/myPlaylists', component: PageMyPlaylistsComponent, canActivate: [UserGuard] }, + { path: 'user/history', component: PageHistoryUserComponent, canActivate: [UserGuard] }, + { path: 'user/myProfil', component: PageProfilUserComponent, canActivate: [UserGuard] }, + { path: 'user/watching', component: PageWatchingVideoComponent, canActivate: [UserGuard] }, // Advertiser - { path: 'advertiser', component: PageAdListAdvertiserComponent }, - { path: 'advertiser/adList', component: PageAdListAdvertiserComponent }, - { path: 'advertiser/myProfil', component: PageProfilAdvertiserComponent }, - { path: 'advertiser/adsPopularity', component: PagesPopularityComponent }, - { path: 'advertiser/subjectsPopularity', component: PagesPopularityComponent }, + { path: 'advertiser', component: PageAdListAdvertiserComponent, canActivate: [AdvertiserGuard] }, + { path: 'advertiser/adList', component: PageAdListAdvertiserComponent, canActivate: [AdvertiserGuard] }, + { path: 'advertiser/myProfil', component: PageProfilAdvertiserComponent, canActivate: [AdvertiserGuard] }, + { path: 'advertiser/adsPopularity', component: PagesPopularityComponent, canActivate: [AdvertiserGuard] }, + { path: 'advertiser/subjectsPopularity', component: PagesPopularityComponent, canActivate: [AdvertiserGuard] }, ]; @NgModule({ diff --git a/src/app/beforeConnexion/login/page-login/page-login.component.ts b/src/app/beforeConnexion/login/page-login/page-login.component.ts index 555e496..0764280 100644 --- a/src/app/beforeConnexion/login/page-login/page-login.component.ts +++ b/src/app/beforeConnexion/login/page-login/page-login.component.ts @@ -60,9 +60,15 @@ export class PageLoginComponent implements OnInit else { this.profilService.setId(retour.data.id); this.profilService.setProfileImageUrl(retour.data.profileImageUrl); - if(retour.data.role.name === "user") this.router.navigateByUrl( '/user/search'); - else if(retour.data.role.name === "advertiser") this.router.navigateByUrl( '/advertiser/adList'); - else if(retour.data.role.name === "admin" || retour.data.role.name === "superAdmin") this.router.navigateByUrl( '/admin/userList'); + if(retour.data.role.name === "user") { + this.profilService.setRole("user"); + this.router.navigateByUrl( '/user/search'); + } + else if(retour.data.role.name === "advertiser") { + this.profilService.setRole("advertiser"); + this.router.navigateByUrl( '/advertiser/adList'); + } + //else if(retour.data.role.name === "admin" || retour.data.role.name === "superAdmin") this.router.navigateByUrl( '/admin/userList'); } } diff --git a/src/app/user/utils/components/navbar-user/navbar-user.component.ts b/src/app/user/utils/components/navbar-user/navbar-user.component.ts index ec8576e..5eef45d 100644 --- a/src/app/user/utils/components/navbar-user/navbar-user.component.ts +++ b/src/app/user/utils/components/navbar-user/navbar-user.component.ts @@ -36,6 +36,7 @@ export class NavbarUserComponent onDeconnexionCallback(retour: any): void { if(retour.status !== "success") console.log(retour); + this.profilService.setRole(""); } } diff --git a/src/app/utils/guards/advertiser/advertiser.guard.spec.ts b/src/app/utils/guards/advertiser/advertiser.guard.spec.ts new file mode 100644 index 0000000..7c2e7d1 --- /dev/null +++ b/src/app/utils/guards/advertiser/advertiser.guard.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { AdvertiserGuard } from './advertiser.guard'; + +describe('AdvertiserGuard', () => { + let guard: AdvertiserGuard; + + beforeEach(() => { + TestBed.configureTestingModule({}); + guard = TestBed.inject(AdvertiserGuard); + }); + + it('should be created', () => { + expect(guard).toBeTruthy(); + }); +}); diff --git a/src/app/utils/guards/advertiser/advertiser.guard.ts b/src/app/utils/guards/advertiser/advertiser.guard.ts new file mode 100644 index 0000000..9278df5 --- /dev/null +++ b/src/app/utils/guards/advertiser/advertiser.guard.ts @@ -0,0 +1,24 @@ +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 AdvertiserGuard implements CanActivate +{ + + constructor(private profilService: ProfilService, private router: Router) {} + + canActivate( route: ActivatedRouteSnapshot, + state: RouterStateSnapshot ): Observable | Promise | boolean | UrlTree + { + if(this.profilService.getRole() === "advertiser") return true; + else { + this.router.navigateByUrl("login"); + return false; + } + } + +} diff --git a/src/app/utils/guards/user/user.guard.spec.ts b/src/app/utils/guards/user/user.guard.spec.ts new file mode 100644 index 0000000..a657320 --- /dev/null +++ b/src/app/utils/guards/user/user.guard.spec.ts @@ -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(); + }); +}); diff --git a/src/app/utils/guards/user/user.guard.ts b/src/app/utils/guards/user/user.guard.ts new file mode 100644 index 0000000..5f4508c --- /dev/null +++ b/src/app/utils/guards/user/user.guard.ts @@ -0,0 +1,24 @@ +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 UserGuard implements CanActivate +{ + + constructor(private profilService: ProfilService, private router: Router) {} + + canActivate( route: ActivatedRouteSnapshot, + state: RouterStateSnapshot ): Observable | Promise | boolean | UrlTree + { + if(this.profilService.getRole() === "user") return true; + else { + this.router.navigateByUrl("login"); + return false; + } + } + +} diff --git a/src/app/utils/profil/profil.service.ts b/src/app/utils/profil/profil.service.ts index 4bbe5ea..5f1ec73 100644 --- a/src/app/utils/profil/profil.service.ts +++ b/src/app/utils/profil/profil.service.ts @@ -6,23 +6,27 @@ import { Injectable } from '@angular/core'; export class ProfilService { - getId(): string - { + getId(): string { return localStorage.getItem('id'); } - getProfileImageUrl(): string - { + getRole(): string { + return localStorage.getItem('role'); + } + + getProfileImageUrl(): string { return localStorage.getItem('profileImageUrl'); } - setId(id: string): void - { + setId(id: string): void { localStorage.setItem('id', id); } - setProfileImageUrl(profileImageUrl: string): void - { + setRole(role: string): void { + localStorage.setItem('role', role); + } + + setProfileImageUrl(profileImageUrl: string): void { localStorage.setItem('profileImageUrl', profileImageUrl); }