feat(login): Added login page within application with links to CIAM login-functionality. (TV-595)

Merge in TEA/mina-sidor-fa-web from feature/TV-595-ciam-login-page to develop

Squashed commit of the following:

commit 7796cbc958bfb14dccb6cfc329fb223b66643af1
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Wed Nov 17 09:46:47 2021 +0100

    Using guard to check if user is logged in

commit 43b9fca3d0d640b5c9711ec9837222ac2df5c782
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Wed Nov 17 08:32:10 2021 +0100

    Added link-button as logout link to my account

commit ab40fae0d4741ee30af146a41ce254c6c7f6658a
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Wed Nov 17 08:25:04 2021 +0100

    Refactored authentication a bit

commit d1c75864f2a0b1867b372655e81e37b28a067503
Merge: 45f35088 8f05343e
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Wed Nov 17 07:04:32 2021 +0100

    Merge branch 'develop' into feature/TV-595-ciam-login-page

commit 45f3508811de2842af1c095ff72949b619d5bc8d
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Nov 16 16:28:44 2021 +0100

    Added resolve to check if user already is logged in when navigating to login page

commit 44b212fb1e0eab7fdb823a8f41ea0d780c920ee0
Merge: 56ed0e57 54ac27ef
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Nov 16 13:58:58 2021 +0100

    Merge branch 'develop' into feature/TV-595-ciam-login-page

commit 56ed0e57fb3f19c4c41ec3fe676db41ed5831557
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Nov 16 13:48:53 2021 +0100

    Implemented custom login page

commit 27a514758d73d685e80a37e490646a759783d1f5
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Nov 16 11:51:57 2021 +0100

    WIP
This commit is contained in:
Erik Tiekstra
2021-11-17 12:06:37 +01:00
parent fac16f0bfc
commit 5aec476719
22 changed files with 302 additions and 95 deletions
@@ -1,9 +1,8 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router } from '@angular/router';
import { environment } from '@msfa-environment';
import { AuthenticationService } from '@msfa-services/api/authentication.service';
import { Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root',
@@ -12,38 +11,27 @@ export class AuthGuard implements CanActivate {
constructor(private authenticationService: AuthenticationService, private router: Router) {}
canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
return this.authenticationService.isLoggedIn$.pipe(
switchMap(loggedIn => {
if (loggedIn) {
return this.authenticationService.isTokenValid$.pipe(
switchMap(({ isValid, isRefreshable }) => {
if (isValid) {
return of(true);
}
if (isRefreshable) {
return this.authenticationService.refreshToken$();
}
this.redirectToLoginPage();
return of(false);
})
);
} else if (route.queryParams.code) {
return this.authenticationService.login$(route.queryParams.code).pipe(map(result => !!result));
}
const isLoggedIn = this.authenticationService.isLoggedIn;
const isTokenValid = this.authenticationService.isTokenValid;
const isTokenRefreshable = this.authenticationService.isTokenRefreshable;
this.redirectToLoginPage();
return of(false);
})
);
if (isLoggedIn) {
if (isTokenValid) {
return of(true);
}
if (isTokenRefreshable) {
return this.authenticationService.refreshToken$();
}
} else if (route.queryParams.code) {
return this.authenticationService.login$(route.queryParams.code).pipe(map(result => !!result));
}
this.redirectToLoginPage();
return of(false);
}
redirectToLoginPage(): void {
this.authenticationService.removeLocalStorageData();
if (environment.ciam.clientId) {
window.location.href = `${environment.ciam.loginUrl}&client_id=${environment.ciam.clientId}&redirect_uri=${window.location.origin}`;
} else {
void this.router.navigateByUrl(environment.ciam.loginUrl);
}
void this.router.navigateByUrl('/logga-in');
}
}
@@ -0,0 +1,19 @@
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { AuthenticationService } from '@msfa-services/api/authentication.service';
@Injectable({
providedIn: 'root',
})
export class SkipIfLoggedInGuard implements CanActivate {
constructor(private authenticationService: AuthenticationService, private router: Router) {}
canActivate(): boolean {
if (this.authenticationService.isLoggedInWithValidToken) {
void this.router.navigateByUrl('/');
return false;
}
return true;
}
}