From d4447933f8521dd546f81e331fdb0e399e042d16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A5rdebrink?= Date: Fri, 20 Aug 2021 10:30:18 +0200 Subject: [PATCH] Merge pull request #60 in TEA/dafa-web-monorepo from bugs/TV-397 to develop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Squashed commit of the following: commit 4213320a89111b3486ff799dbdd9b648a245fc00 Author: arbetsformedlingen_garcn Date: Fri Aug 20 09:27:04 2021 +0200 TV-397 tog bort metoder som fanns i tvÄ servicar. commit 01195a0b2b5ea8da234c5e32692b51aff32b23e6 Merge: 6b5b1e9 1e2d925 Author: arbetsformedlingen_garcn Date: Fri Aug 20 09:24:01 2021 +0200 Merge branch 'develop-remote' into bugs/TV-397 commit 6b5b1e9fbad2c68ddbd38f0a3c5e183fc6a707c4 Author: arbetsformedlingen_garcn Date: Thu Aug 19 15:42:36 2021 +0200 TV-397 fixed bug that occurs if userService is brought in as a dependency before authentication is done. --- .../src/app/pages/logout/logout.component.ts | 4 +--- .../app/shared/constants/local-storage-keys.ts | 1 + .../src/app/shared/guards/auth.guard.ts | 9 ++------- .../shared/services/api/authentication.service.ts | 6 ++++++ .../src/app/shared/services/api/user.service.ts | 15 +++------------ 5 files changed, 13 insertions(+), 22 deletions(-) create mode 100644 apps/mina-sidor-fa/src/app/shared/constants/local-storage-keys.ts diff --git a/apps/mina-sidor-fa/src/app/pages/logout/logout.component.ts b/apps/mina-sidor-fa/src/app/pages/logout/logout.component.ts index f21f371..58884a9 100644 --- a/apps/mina-sidor-fa/src/app/pages/logout/logout.component.ts +++ b/apps/mina-sidor-fa/src/app/pages/logout/logout.component.ts @@ -1,5 +1,4 @@ import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; -import { UserService } from '@msfa-services/api/user.service'; import { environment } from '@msfa-environment'; import { AuthenticationService } from '@msfa-services/api/authentication.service'; @@ -12,10 +11,9 @@ import { AuthenticationService } from '@msfa-services/api/authentication.service export class LogoutComponent implements OnInit { loginUrl = environment.loginUrl; - constructor(private authenticationService: AuthenticationService, private userService: UserService) {} + constructor(private authenticationService: AuthenticationService) {} ngOnInit(): void { this.authenticationService.logout(); - this.userService.removeSelectedUserOrganization(); } } diff --git a/apps/mina-sidor-fa/src/app/shared/constants/local-storage-keys.ts b/apps/mina-sidor-fa/src/app/shared/constants/local-storage-keys.ts new file mode 100644 index 0000000..06c9479 --- /dev/null +++ b/apps/mina-sidor-fa/src/app/shared/constants/local-storage-keys.ts @@ -0,0 +1 @@ +export const selectedUserOrganizationNumberKey = 'selectedOrganizationId'; diff --git a/apps/mina-sidor-fa/src/app/shared/guards/auth.guard.ts b/apps/mina-sidor-fa/src/app/shared/guards/auth.guard.ts index 0032fbb..482fee4 100644 --- a/apps/mina-sidor-fa/src/app/shared/guards/auth.guard.ts +++ b/apps/mina-sidor-fa/src/app/shared/guards/auth.guard.ts @@ -1,6 +1,5 @@ import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, CanActivate, Router } from '@angular/router'; -import { UserService } from '@msfa-services/api/user.service'; import { environment } from '@msfa-environment'; import { AuthenticationService } from '@msfa-services/api/authentication.service'; import { Observable, of } from 'rxjs'; @@ -9,17 +8,13 @@ import { redirectUriQueryParam } from '../../pages/organization-picker/organizat @Injectable() export class AuthGuard implements CanActivate { - constructor( - private authenticationService: AuthenticationService, - private userService: UserService, - private router: Router - ) {} + constructor(private authenticationService: AuthenticationService, private router: Router) {} canActivate(route: ActivatedRouteSnapshot): Observable { return this.authenticationService.isLoggedIn$.pipe( switchMap(loggedIn => { if (loggedIn) { - if (!this.userService.hasSelectedUserOrganization()) { + if (!this.authenticationService.hasSelectedUserOrganization()) { this.router.navigateByUrl(`/organization-picker?${redirectUriQueryParam}=${encodeURI(location.href)}`); } diff --git a/apps/mina-sidor-fa/src/app/shared/services/api/authentication.service.ts b/apps/mina-sidor-fa/src/app/shared/services/api/authentication.service.ts index e9de472..0f20361 100644 --- a/apps/mina-sidor-fa/src/app/shared/services/api/authentication.service.ts +++ b/apps/mina-sidor-fa/src/app/shared/services/api/authentication.service.ts @@ -1,6 +1,7 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; +import { selectedUserOrganizationNumberKey } from '@msfa-constants/local-storage-keys'; import { environment } from '@msfa-environment'; import { AuthenticationResponse } from '@msfa-models/api/authentication.response.model'; import { Authentication, mapAuthApiResponseToAuthenticationResult } from '@msfa-models/authentication.model'; @@ -82,6 +83,7 @@ export class AuthenticationService { logout(): void { localStorage.removeItem('id_token'); localStorage.removeItem('expires_at'); + localStorage.removeItem(selectedUserOrganizationNumberKey); if (environment.environment === 'local') { void this.router.navigateByUrl(environment.logoutUrl); @@ -89,4 +91,8 @@ export class AuthenticationService { document.location.href = environment.logoutUrl; } } + + hasSelectedUserOrganization(): boolean { + return !!localStorage.getItem(selectedUserOrganizationNumberKey); + } } diff --git a/apps/mina-sidor-fa/src/app/shared/services/api/user.service.ts b/apps/mina-sidor-fa/src/app/shared/services/api/user.service.ts index 5486db1..0ec0375 100644 --- a/apps/mina-sidor-fa/src/app/shared/services/api/user.service.ts +++ b/apps/mina-sidor-fa/src/app/shared/services/api/user.service.ts @@ -1,5 +1,6 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; +import { selectedUserOrganizationNumberKey } from '@msfa-constants/local-storage-keys'; import { UnsubscribeDirective } from '@msfa-directives/unsubscribe.directive'; import { environment } from '@msfa-environment'; import { OrganizationResponse } from '@msfa-models/api/organization.response.model'; @@ -16,8 +17,6 @@ const API_HEADERS = { headers: environment.api.headers }; providedIn: 'root', }) export class UserService extends UnsubscribeDirective { - private readonly selectedUserOrganizationNumberKey = 'selectedOrganizationId'; - private _authApiUrl = `${environment.api.url}/auth`; private _user$ = new BehaviorSubject(null); @@ -38,7 +37,7 @@ export class UserService extends UnsubscribeDirective { } return user.organizations.find( - organization => organization.organizationNumber === localStorage.getItem(this.selectedUserOrganizationNumberKey) + organization => organization.organizationNumber === localStorage.getItem(selectedUserOrganizationNumberKey) ); } @@ -47,15 +46,7 @@ export class UserService extends UnsubscribeDirective { return; } - localStorage.setItem(this.selectedUserOrganizationNumberKey, organization?.organizationNumber); - } - - hasSelectedUserOrganization(): boolean { - return !!localStorage.getItem(this.selectedUserOrganizationNumberKey); - } - - removeSelectedUserOrganization(): void { - localStorage.removeItem(this.selectedUserOrganizationNumberKey); + localStorage.setItem(selectedUserOrganizationNumberKey, organization?.organizationNumber); } private _fetchOrganizations$(): Observable {