From b9beb70bd38947412f4aa24c711743fd6ca2ed19 Mon Sep 17 00:00:00 2001
From: Etienne LOUPIAS <eloupias@grandlyon.com>
Date: Tue, 26 Mar 2024 15:49:31 +0100
Subject: [PATCH] fix(structure): edit page reload redirect to home

---
 src/app/guards/role.guard.ts                | 6 +++---
 src/app/profile/services/profile.service.ts | 7 ++++---
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/app/guards/role.guard.ts b/src/app/guards/role.guard.ts
index a98f13aed..216256537 100644
--- a/src/app/guards/role.guard.ts
+++ b/src/app/guards/role.guard.ts
@@ -1,5 +1,5 @@
-import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router';
 import { Injectable } from '@angular/core';
+import { ActivatedRouteSnapshot, Router, UrlTree } from '@angular/router';
 import { ProfileService } from '../profile/services/profile.service';
 import { AuthService } from '../services/auth.service';
 import { RouteRole } from '../shared/enum/routeRole.enum';
@@ -15,14 +15,14 @@ export class RoleGuard {
     private authService: AuthService,
   ) {}
 
-  canActivate(route: ActivatedRouteSnapshot): UrlTree | boolean {
+  async canActivate(route: ActivatedRouteSnapshot): Promise<UrlTree | boolean> {
     const allowedRoles = route.data.allowedRoles;
 
     if (this.authService.isLoggedIn()) {
       if (this.profileService.isAdmin()) return true;
       if (allowedRoles.includes(RouteRole.structureAdmin)) {
         const structureId = route.params.id;
-        if (this.profileService.isLinkedToStructure(structureId)) {
+        if (await this.profileService.isLinkedToStructure(structureId)) {
           return true;
         }
       }
diff --git a/src/app/profile/services/profile.service.ts b/src/app/profile/services/profile.service.ts
index b740c65af..363580320 100644
--- a/src/app/profile/services/profile.service.ts
+++ b/src/app/profile/services/profile.service.ts
@@ -1,7 +1,7 @@
 import { HttpClient } from '@angular/common/http';
 import { Injectable } from '@angular/core';
 import decode from 'jwt-decode';
-import { Observable } from 'rxjs';
+import { Observable, lastValueFrom } from 'rxjs';
 import { catchError, map } from 'rxjs/operators';
 import { Employer } from '../../models/employer.model';
 import { Job } from '../../models/job.model';
@@ -25,7 +25,7 @@ export class ProfileService {
 
   public async getProfile(): Promise<User> {
     if (this.authService.isLoggedIn()) {
-      const profile = await this.http.get<User>(`${this.baseUrl}/profile`).toPromise();
+      const profile = await lastValueFrom(this.http.get<User>(`${this.baseUrl}/profile`));
       this.currentProfile = profile;
     }
     return this.currentProfile;
@@ -38,7 +38,8 @@ export class ProfileService {
     return this.http.delete<User>(`${this.baseUrl}`);
   }
 
-  public isLinkedToStructure(idStructure: string): boolean {
+  public async isLinkedToStructure(idStructure: string): Promise<boolean> {
+    await this.getProfile();
     if (!this.currentProfile) {
       return false;
     }
-- 
GitLab