From 4c0720ebf58e4ec699011a20866fa097a0bc614b Mon Sep 17 00:00:00 2001 From: John Hoffmann Date: Sat, 2 Nov 2024 08:54:12 +0100 Subject: [PATCH 1/4] fix: reactive login state handling --- src/app/app.component.html | 4 ++-- src/app/app.component.ts | 14 +++++------ .../authentication.component.html | 2 +- .../authentication.component.ts | 22 +++++++++++------- src/app/services/auth.service.ts | 23 +++++++++++-------- 5 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/app/app.component.html b/src/app/app.component.html index 63a2559..8016415 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -4,7 +4,7 @@ Browse Catalog Query Tool - + @@ -16,7 +16,7 @@
- +
diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 3e1804b..bfcf8e8 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -8,27 +8,27 @@ import { Subscription } from 'rxjs'; styleUrls: ['./app.component.scss'], }) export class AppComponent implements OnInit, OnDestroy { + isLoggedIn = false; username: string | null = null; + private loginStateSubscription?: Subscription; private usernameSubscription?: Subscription; constructor(private authService: AuthService) {} ngOnInit(): void { + this.loginStateSubscription = this.authService.isLoggedIn$.subscribe((status) => { + this.isLoggedIn = status; + }); + this.usernameSubscription = this.authService.username$.subscribe((username) => { this.username = username; }); - - if (this.isLoggedIn()) { - this.username = this.authService.getUsername(); - } } ngOnDestroy(): void { + this.loginStateSubscription?.unsubscribe(); this.usernameSubscription?.unsubscribe(); } - isLoggedIn(): boolean { - return this.authService.isLoggedIn(); - } logout() { this.authService.logout(); diff --git a/src/app/components/authentication/authentication.component.html b/src/app/components/authentication/authentication.component.html index e1c5b8c..6e72d56 100644 --- a/src/app/components/authentication/authentication.component.html +++ b/src/app/components/authentication/authentication.component.html @@ -1,4 +1,4 @@ -