diff --git a/src/app/guards/role.guard.ts b/src/app/guards/role.guard.ts index a98f13aed407271e04c04cb707247da314a44e6b..2162565376f8fedd99e2d5b1590c0d19ac60ab00 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 b740c65af1271728fcfdd6ec4da186eef7440202..363580320c74cfa41ca72d9f6207a275ba01a907 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; }