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 {