43 lines
946 B
TypeScript
43 lines
946 B
TypeScript
|
import { HttpClient } from '@angular/common/http';
|
||
|
import { Injectable } from '@angular/core';
|
||
|
|
||
|
@Injectable({
|
||
|
providedIn: 'root'
|
||
|
})
|
||
|
export class ApiAuthenticationService {
|
||
|
private authenticated: boolean;
|
||
|
private lastCheck: Date;
|
||
|
|
||
|
constructor(private http: HttpClient) {
|
||
|
this.authenticated = false;
|
||
|
this.lastCheck = new Date();
|
||
|
}
|
||
|
|
||
|
isAuthenticated() {
|
||
|
return this.authenticated;
|
||
|
}
|
||
|
|
||
|
update() {
|
||
|
const jwt = localStorage.getItem('jwt');
|
||
|
if (!jwt) {
|
||
|
this.updateAuthenticated(false);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// /api/auth/jwt
|
||
|
this.http.get('/api/auth/jwt', {
|
||
|
headers: {
|
||
|
'Authorization': 'Bearer ' + jwt
|
||
|
}
|
||
|
}).subscribe((data: any) => {
|
||
|
console.log('jwt validation', data);
|
||
|
this.updateAuthenticated(data?.authenticated);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
private updateAuthenticated(value: boolean) {
|
||
|
this.authenticated = value;
|
||
|
this.lastCheck = new Date();
|
||
|
}
|
||
|
}
|