From f2c5178e82dd9badb2ce4c31e5fd9d9500d6a2b6 Mon Sep 17 00:00:00 2001 From: Tom Date: Mon, 7 Apr 2025 18:49:15 +0000 Subject: [PATCH] Fixed storing JWT in local storage. --- .../services/api/api-authentication.service.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/app/shared/services/api/api-authentication.service.ts b/src/app/shared/services/api/api-authentication.service.ts index 3843564..214c45c 100644 --- a/src/app/shared/services/api/api-authentication.service.ts +++ b/src/app/shared/services/api/api-authentication.service.ts @@ -41,13 +41,12 @@ export class ApiAuthenticationService { } logout() { - localStorage.removeItem('jwt'); - this.updateAuthenticated(false, null); + this.updateAuthenticated(null, false, null); } update(jwt: string | null) { if (!jwt) { - this.updateAuthenticated(false, null); + this.updateAuthenticated(null, false, null); return; } @@ -57,17 +56,23 @@ export class ApiAuthenticationService { }, withCredentials: true }).subscribe({ - next: (data: any) => this.updateAuthenticated(data?.authenticated, data?.user), - error: () => this.updateAuthenticated(false, null) + next: (data: any) => this.updateAuthenticated(jwt, data?.authenticated, data?.user), + error: () => this.updateAuthenticated(jwt, false, null) }); } - private updateAuthenticated(authenticated: boolean, user: any) { + private updateAuthenticated(jwt: string | null, authenticated: boolean, user: any) { const previous = this.authenticated; this.user = user; this.authenticated = authenticated; this.lastCheck = new Date(); + if (jwt) { + localStorage.setItem('jwt', jwt); + } else { + localStorage.removeItem('jwt'); + } + if (previous != authenticated) { if (authenticated) { this.events.emit('login', null);