diff --git a/src/app/app.module.ts b/src/app/app.module.ts index acaba73..c90348e 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -32,7 +32,7 @@ import { FicheContactComponent } from './components/fiche-contact/fiche-contact. import { PaginationComponent } from './components/pagination/pagination.component'; import { CreneauComponent } from './components/creneau/creneau.component'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; -import { HttpClientModule } from '@angular/common/http'; +import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'; import { PageAjoutContactComponent } from './pages/page-ajout-contact/page-ajout-contact.component'; import { PageModifierContactComponent } from './pages/page-modifier-contact/page-modifier-contact.component'; import { PageCreationTeamComponent } from './pages/page-creation-team/page-creation-team.component'; @@ -89,7 +89,9 @@ import { EmoticonComponent } from './emoticon/emoticon.component'; FormsModule, AutofocusFixModule.forRoot(), ], - providers: [], + providers: [ + + ], bootstrap: [AppComponent], }) export class AppModule {} diff --git a/src/app/services/menus.service.ts b/src/app/services/menus.service.ts index 8031c14..a762025 100644 --- a/src/app/services/menus.service.ts +++ b/src/app/services/menus.service.ts @@ -3,8 +3,8 @@ import { HttpClient } from '@angular/common/http'; import { map, Observable } from 'rxjs'; import { environment } from 'src/environments/environment'; import { Menu } from '../models/menu'; -import jwt_decode from 'jwt-decode'; import { Router } from '@angular/router'; +import { TokenService } from './token.service'; @Injectable({ providedIn: 'root' @@ -12,25 +12,18 @@ import { Router } from '@angular/router'; export class MenusService { apiUrl: string; -tokenKey: string; - constructor(private http: HttpClient, private router: Router) { + constructor(private http: HttpClient, private router: Router, private tokenService: TokenService) { // On se sert des variables d'environnement de notre application this.apiUrl = environment.apiUrl; - this.tokenKey = environment.tokenKey; } //on affiche les menus d'une team getMenu(): Observable | void { - //on récupère l'id de la team grâce au token du membre : - const token = localStorage.getItem(this.tokenKey); - if(token){ //s'il y a un token tu le decode - const decodedToken = jwt_decode(token); - const teamId = decodedToken.teamId;//tu vas chercher le teamId - console.log(teamId); //dans le json du token + const teamId = this.tokenService.getCurrentTeamId(); + if (teamId){ return this.http.get(`${this.apiUrl}/menus/team/${teamId}`); - //on reconstruit alors l'url pour afficher les menus via le team_id }else { this.router.navigate(['accueil']); } @@ -46,12 +39,12 @@ tokenKey: string; return this.http.post(`${this.apiUrl}/menus/add`, menu); } - deleteMenu(menu: Menu): Observable { - return this.http.delete(`${this.apiUrl}/menus/delete/1`); + deleteMenu(id: any): Observable { + return this.http.delete(`${this.apiUrl}/menus/delete/`+ id); } updateMenu(menu: Menu): Observable { - return this.http.put(`${this.apiUrl}/contacts/update/1`, menu); + return this.http.put(`${this.apiUrl}/contacts/update/`, menu); } } diff --git a/src/app/services/token.service.spec.ts b/src/app/services/token.service.spec.ts new file mode 100644 index 0000000..7930902 --- /dev/null +++ b/src/app/services/token.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { TokenService } from './token.service'; + +describe('TokenService', () => { + let service: TokenService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(TokenService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/token.service.ts b/src/app/services/token.service.ts new file mode 100644 index 0000000..ef13ff6 --- /dev/null +++ b/src/app/services/token.service.ts @@ -0,0 +1,45 @@ +import { Injectable } from '@angular/core'; +import { environment } from 'src/environments/environment'; +import jwt_decode from 'jwt-decode'; + +@Injectable({ + providedIn: 'root' +}) +export class TokenService { +tokenKey = environment.tokenKey; + + constructor() { } + + public getToken(): string | null { + const token = localStorage.getItem(this.tokenKey); + if(token) { + return token; + } else { + return null; + } + } + + public getCurrentMembreId(): number | null { + const token = this.getToken(); + if(token) { + const decodedToken = jwt_decode(token); + const userId = decodedToken.sub; + return userId; + } else { + return null; + } + } + + public getCurrentTeamId(): number | null { + const token = this.getToken(); + if(token){ + const decodedToken = jwt_decode(token); + const teamId = decodedToken.teamId; + return teamId ; + + }else { +return null; + } + + } +}