Skip to content
Snippets Groups Projects
Commit 8d09372c authored by Jérémie BRISON's avatar Jérémie BRISON Committed by Hugo SUBTIL
Browse files

fix(admin) : fix isAdmin bug when user not logged

parent 422ac4e4
Branches
Tags
3 merge requests!68Recette,!67Dev,!56Feat/admin edit structure
...@@ -4,6 +4,7 @@ import { Observable } from 'rxjs'; ...@@ -4,6 +4,7 @@ import { Observable } from 'rxjs';
import { User } from '../../models/user.model'; import { User } from '../../models/user.model';
import decode from 'jwt-decode'; import decode from 'jwt-decode';
import { UserRole } from '../../shared/enum/userRole.enum'; import { UserRole } from '../../shared/enum/userRole.enum';
import { AuthService } from '../../services/auth.service';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
...@@ -11,7 +12,7 @@ import { UserRole } from '../../shared/enum/userRole.enum'; ...@@ -11,7 +12,7 @@ import { UserRole } from '../../shared/enum/userRole.enum';
export class ProfileService { export class ProfileService {
private readonly baseUrl = 'api/users'; private readonly baseUrl = 'api/users';
private currentProfile: User = null; private currentProfile: User = null;
constructor(private http: HttpClient) { constructor(private http: HttpClient, private authService: AuthService) {
this.getProfile(); this.getProfile();
} }
...@@ -45,14 +46,15 @@ export class ProfileService { ...@@ -45,14 +46,15 @@ export class ProfileService {
} }
public isAdmin(): boolean { public isAdmin(): boolean {
const user = JSON.parse(localStorage.getItem('user')); if (this.authService.isLoggedIn()) {
if (user) { const user = this.authService.userValue;
const token = user.accessToken; const token = user.accessToken;
// decode the token to get its payload // decode the token to get its payload
const tokenPayload: User = decode(token); const tokenPayload: User = decode(token);
if (tokenPayload.role == UserRole.admin) { if (tokenPayload.role == UserRole.admin) {
return true; return true;
} }
return false;
} }
return false; return false;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment