diff --git a/src/app/profile/services/profile.service.ts b/src/app/profile/services/profile.service.ts index b1baf54e7abe8ed37c3e4c711a349a07d1838ecb..511ad60d9a14df9278329a49e7cfb2d74dd573ff 100644 --- a/src/app/profile/services/profile.service.ts +++ b/src/app/profile/services/profile.service.ts @@ -1,6 +1,5 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; -import decode from 'jwt-decode'; import { Observable, lastValueFrom } from 'rxjs'; import { catchError, map } from 'rxjs/operators'; import { Employer } from '../../models/employer.model'; @@ -23,6 +22,14 @@ export class ProfileService { private notificationService: NotificationService, ) {} + public getId(): string { + if (this.authService.isLoggedIn()) { + const tokenPayload = this.authService.tokenPayload; + return tokenPayload?.id; + } + return null; + } + public async getProfile(): Promise<User> { if (this.authService.isLoggedIn()) { const profile = await lastValueFrom(this.http.get<User>(`${this.baseUrl}/profile`)); @@ -55,10 +62,7 @@ export class ProfileService { public isAdmin(): boolean { if (this.authService.isLoggedIn()) { - const user = this.authService.userValue; - const token = user.accessToken; - // decode the token to get its payload - const tokenPayload: User = decode(token); + const tokenPayload = this.authService.tokenPayload; if (tokenPayload.role === UserRole.admin) { return true; } diff --git a/src/app/profile/structure-edition-summary/structure-edition-summary.component.ts b/src/app/profile/structure-edition-summary/structure-edition-summary.component.ts index 1a7468287e3c1483578028c3e0cb1eda7aff93e9..6430bef42a3fd6e5d8e6c18579840408b727d2fd 100644 --- a/src/app/profile/structure-edition-summary/structure-edition-summary.component.ts +++ b/src/app/profile/structure-edition-summary/structure-edition-summary.component.ts @@ -101,8 +101,8 @@ export class StructureEditionSummaryComponent implements OnInit { // Get categories labels if (personalOffer) { - // TODO : recuperer si c'est le user connecté en utilisant le profileService, mais a priori le profileService a pas l'id du user... - const isMyself = true; + // Check if it is the user's own offer + const isMyself = this.profileService.getId() === member._id; const offerHolder = { user: member, diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 6e4382da4a0d22a1dcd03e8415be4fe09303329d..afe7b881a2c85857af2968380594a194c675f021 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -1,5 +1,6 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; +import decode from 'jwt-decode'; import { DateTime } from 'luxon'; import { BehaviorSubject, Observable } from 'rxjs'; import { map } from 'rxjs/operators'; @@ -33,6 +34,15 @@ export class AuthService { return null; } + public get tokenPayload(): User { + const token = this.token; + if (!token) return null; + + // decode the token to get its payload + const tokenPayload: User = decode(token); + return tokenPayload; + } + public logout(): void { localStorage.removeItem('user'); this.userSubject.next(null);