ajout de la guard

This commit is contained in:
MiharyR 2021-12-24 15:58:45 +01:00
parent c18fd4c24d
commit 2d95837985
9 changed files with 116 additions and 22 deletions

View file

@ -36,6 +36,7 @@ export class NavbarAdvertiserComponent
onDeconnexionCallback(retour: any): void
{
if(retour.status !== "success") console.log(retour);
this.profilService.setRole("");
}
}

View file

@ -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({

View file

@ -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');
}
}

View file

@ -36,6 +36,7 @@ export class NavbarUserComponent
onDeconnexionCallback(retour: any): void
{
if(retour.status !== "success") console.log(retour);
this.profilService.setRole("");
}
}

View file

@ -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();
});
});

View file

@ -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<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
{
if(this.profilService.getRole() === "advertiser") return true;
else {
this.router.navigateByUrl("login");
return false;
}
}
}

View 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();
});
});

View file

@ -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<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
{
if(this.profilService.getRole() === "user") return true;
else {
this.router.navigateByUrl("login");
return false;
}
}
}

View file

@ -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);
}