From 8d09372ccc8e833e1417e84a0569b5f8eb6095a4 Mon Sep 17 00:00:00 2001
From: Jeremie BRISON <ext.sopra.jbrison@grandlyon.com>
Date: Mon, 18 Jan 2021 18:09:29 +0100
Subject: [PATCH] fix(admin) : fix isAdmin bug when user not logged

---
 src/app/profile/services/profile.service.ts | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/app/profile/services/profile.service.ts b/src/app/profile/services/profile.service.ts
index 87c91ca9f..79abef7df 100644
--- a/src/app/profile/services/profile.service.ts
+++ b/src/app/profile/services/profile.service.ts
@@ -4,6 +4,7 @@ import { Observable } from 'rxjs';
 import { User } from '../../models/user.model';
 import decode from 'jwt-decode';
 import { UserRole } from '../../shared/enum/userRole.enum';
+import { AuthService } from '../../services/auth.service';
 
 @Injectable({
   providedIn: 'root',
@@ -11,7 +12,7 @@ import { UserRole } from '../../shared/enum/userRole.enum';
 export class ProfileService {
   private readonly baseUrl = 'api/users';
   private currentProfile: User = null;
-  constructor(private http: HttpClient) {
+  constructor(private http: HttpClient, private authService: AuthService) {
     this.getProfile();
   }
 
@@ -45,14 +46,15 @@ export class ProfileService {
   }
 
   public isAdmin(): boolean {
-    const user = JSON.parse(localStorage.getItem('user'));
-    if (user) {
+    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);
       if (tokenPayload.role == UserRole.admin) {
         return true;
       }
+      return false;
     }
     return false;
   }
-- 
GitLab