ajout de la guard
This commit is contained in:
parent
c18fd4c24d
commit
2d95837985
9 changed files with 116 additions and 22 deletions
|
|
@ -36,6 +36,7 @@ export class NavbarAdvertiserComponent
|
||||||
onDeconnexionCallback(retour: any): void
|
onDeconnexionCallback(retour: any): void
|
||||||
{
|
{
|
||||||
if(retour.status !== "success") console.log(retour);
|
if(retour.status !== "success") console.log(retour);
|
||||||
|
this.profilService.setRole("");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 {PageAdListAdvertiserComponent} from "./advertiser/adList/page-ad-list-advertiser/page-ad-list-advertiser.component";
|
||||||
import {PagesPopularityComponent} from "./advertiser/pages-popularity/pages-popularity.component";
|
import {PagesPopularityComponent} from "./advertiser/pages-popularity/pages-popularity.component";
|
||||||
import {PageProfilAdvertiserComponent} from "./advertiser/myProfil/page-profil-advertiser/page-profil-advertiser.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 = [
|
const routes: Routes = [
|
||||||
|
|
@ -19,19 +21,19 @@ const routes: Routes = [
|
||||||
{ path: 'register', component: PageRegisterComponent },
|
{ path: 'register', component: PageRegisterComponent },
|
||||||
|
|
||||||
// User
|
// User
|
||||||
{ path: 'user', component: PageSearchComponent },
|
{ path: 'user', component: PageSearchComponent, canActivate: [UserGuard] },
|
||||||
{ path: 'user/search', component: PageSearchComponent },
|
{ path: 'user/search', component: PageSearchComponent, canActivate: [UserGuard] },
|
||||||
{ path: 'user/myPlaylists', component: PageMyPlaylistsComponent },
|
{ path: 'user/myPlaylists', component: PageMyPlaylistsComponent, canActivate: [UserGuard] },
|
||||||
{ path: 'user/history', component: PageHistoryUserComponent },
|
{ path: 'user/history', component: PageHistoryUserComponent, canActivate: [UserGuard] },
|
||||||
{ path: 'user/myProfil', component: PageProfilUserComponent },
|
{ path: 'user/myProfil', component: PageProfilUserComponent, canActivate: [UserGuard] },
|
||||||
{ path: 'user/watching', component: PageWatchingVideoComponent },
|
{ path: 'user/watching', component: PageWatchingVideoComponent, canActivate: [UserGuard] },
|
||||||
|
|
||||||
// Advertiser
|
// Advertiser
|
||||||
{ path: 'advertiser', component: PageAdListAdvertiserComponent },
|
{ path: 'advertiser', component: PageAdListAdvertiserComponent, canActivate: [AdvertiserGuard] },
|
||||||
{ path: 'advertiser/adList', component: PageAdListAdvertiserComponent },
|
{ path: 'advertiser/adList', component: PageAdListAdvertiserComponent, canActivate: [AdvertiserGuard] },
|
||||||
{ path: 'advertiser/myProfil', component: PageProfilAdvertiserComponent },
|
{ path: 'advertiser/myProfil', component: PageProfilAdvertiserComponent, canActivate: [AdvertiserGuard] },
|
||||||
{ path: 'advertiser/adsPopularity', component: PagesPopularityComponent },
|
{ path: 'advertiser/adsPopularity', component: PagesPopularityComponent, canActivate: [AdvertiserGuard] },
|
||||||
{ path: 'advertiser/subjectsPopularity', component: PagesPopularityComponent },
|
{ path: 'advertiser/subjectsPopularity', component: PagesPopularityComponent, canActivate: [AdvertiserGuard] },
|
||||||
];
|
];
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
|
|
|
||||||
|
|
@ -60,9 +60,15 @@ export class PageLoginComponent implements OnInit
|
||||||
else {
|
else {
|
||||||
this.profilService.setId(retour.data.id);
|
this.profilService.setId(retour.data.id);
|
||||||
this.profilService.setProfileImageUrl(retour.data.profileImageUrl);
|
this.profilService.setProfileImageUrl(retour.data.profileImageUrl);
|
||||||
if(retour.data.role.name === "user") this.router.navigateByUrl( '/user/search');
|
if(retour.data.role.name === "user") {
|
||||||
else if(retour.data.role.name === "advertiser") this.router.navigateByUrl( '/advertiser/adList');
|
this.profilService.setRole("user");
|
||||||
else if(retour.data.role.name === "admin" || retour.data.role.name === "superAdmin") this.router.navigateByUrl( '/admin/userList');
|
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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ export class NavbarUserComponent
|
||||||
onDeconnexionCallback(retour: any): void
|
onDeconnexionCallback(retour: any): void
|
||||||
{
|
{
|
||||||
if(retour.status !== "success") console.log(retour);
|
if(retour.status !== "success") console.log(retour);
|
||||||
|
this.profilService.setRole("");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
16
src/app/utils/guards/advertiser/advertiser.guard.spec.ts
Normal file
16
src/app/utils/guards/advertiser/advertiser.guard.spec.ts
Normal 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();
|
||||||
|
});
|
||||||
|
});
|
||||||
24
src/app/utils/guards/advertiser/advertiser.guard.ts
Normal file
24
src/app/utils/guards/advertiser/advertiser.guard.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
16
src/app/utils/guards/user/user.guard.spec.ts
Normal file
16
src/app/utils/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();
|
||||||
|
});
|
||||||
|
});
|
||||||
24
src/app/utils/guards/user/user.guard.ts
Normal file
24
src/app/utils/guards/user/user.guard.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -6,23 +6,27 @@ import { Injectable } from '@angular/core';
|
||||||
export class ProfilService
|
export class ProfilService
|
||||||
{
|
{
|
||||||
|
|
||||||
getId(): string
|
getId(): string {
|
||||||
{
|
|
||||||
return localStorage.getItem('id');
|
return localStorage.getItem('id');
|
||||||
}
|
}
|
||||||
|
|
||||||
getProfileImageUrl(): string
|
getRole(): string {
|
||||||
{
|
return localStorage.getItem('role');
|
||||||
|
}
|
||||||
|
|
||||||
|
getProfileImageUrl(): string {
|
||||||
return localStorage.getItem('profileImageUrl');
|
return localStorage.getItem('profileImageUrl');
|
||||||
}
|
}
|
||||||
|
|
||||||
setId(id: string): void
|
setId(id: string): void {
|
||||||
{
|
|
||||||
localStorage.setItem('id', id);
|
localStorage.setItem('id', id);
|
||||||
}
|
}
|
||||||
|
|
||||||
setProfileImageUrl(profileImageUrl: string): void
|
setRole(role: string): void {
|
||||||
{
|
localStorage.setItem('role', role);
|
||||||
|
}
|
||||||
|
|
||||||
|
setProfileImageUrl(profileImageUrl: string): void {
|
||||||
localStorage.setItem('profileImageUrl', profileImageUrl);
|
localStorage.setItem('profileImageUrl', profileImageUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Reference in a new issue