Merge pull request #60 in TEA/dafa-web-monorepo from bugs/TV-397 to develop

Squashed commit of the following:

commit 4213320a89111b3486ff799dbdd9b648a245fc00
Author: arbetsformedlingen_garcn <christian.gardebrink@arbetsformedlingen.se>
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 <christian.gardebrink@arbetsformedlingen.se>
Date:   Fri Aug 20 09:24:01 2021 +0200

    Merge branch 'develop-remote' into bugs/TV-397

commit 6b5b1e9fbad2c68ddbd38f0a3c5e183fc6a707c4
Author: arbetsformedlingen_garcn <christian.gardebrink@arbetsformedlingen.se>
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.
This commit is contained in:
Christian Gårdebrink
2021-08-20 10:30:18 +02:00
parent 1e2d925d27
commit d4447933f8
5 changed files with 13 additions and 22 deletions

View File

@@ -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();
}
}

View File

@@ -0,0 +1 @@
export const selectedUserOrganizationNumberKey = 'selectedOrganizationId';

View File

@@ -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<boolean> {
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)}`);
}

View File

@@ -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);
}
}

View File

@@ -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<User>(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<Organization[]> {