Fixed storing JWT in local storage.
This commit is contained in:
parent
7048a7c46c
commit
f2c5178e82
@ -41,13 +41,12 @@ export class ApiAuthenticationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
logout() {
|
logout() {
|
||||||
localStorage.removeItem('jwt');
|
this.updateAuthenticated(null, false, null);
|
||||||
this.updateAuthenticated(false, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
update(jwt: string | null) {
|
update(jwt: string | null) {
|
||||||
if (!jwt) {
|
if (!jwt) {
|
||||||
this.updateAuthenticated(false, null);
|
this.updateAuthenticated(null, false, null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,17 +56,23 @@ export class ApiAuthenticationService {
|
|||||||
},
|
},
|
||||||
withCredentials: true
|
withCredentials: true
|
||||||
}).subscribe({
|
}).subscribe({
|
||||||
next: (data: any) => this.updateAuthenticated(data?.authenticated, data?.user),
|
next: (data: any) => this.updateAuthenticated(jwt, data?.authenticated, data?.user),
|
||||||
error: () => this.updateAuthenticated(false, null)
|
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;
|
const previous = this.authenticated;
|
||||||
this.user = user;
|
this.user = user;
|
||||||
this.authenticated = authenticated;
|
this.authenticated = authenticated;
|
||||||
this.lastCheck = new Date();
|
this.lastCheck = new Date();
|
||||||
|
|
||||||
|
if (jwt) {
|
||||||
|
localStorage.setItem('jwt', jwt);
|
||||||
|
} else {
|
||||||
|
localStorage.removeItem('jwt');
|
||||||
|
}
|
||||||
|
|
||||||
if (previous != authenticated) {
|
if (previous != authenticated) {
|
||||||
if (authenticated) {
|
if (authenticated) {
|
||||||
this.events.emit('login', null);
|
this.events.emit('login', null);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user