Fixed storing JWT in local storage.

This commit is contained in:
Tom 2025-04-07 18:49:15 +00:00
parent 7048a7c46c
commit f2c5178e82

View File

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