Ported:
Guest Book Save bill Update bill Show bill Fix: Tax details was sending the rate multiplied by 100 but not dividing it on save/update Feature: Updated the auth service to check local storage for the latest user token
This commit is contained in:
@ -16,22 +16,39 @@ export class AuthService {
|
||||
public currentUser: Observable<User>;
|
||||
|
||||
constructor(private http: HttpClient) {
|
||||
this.currentUserSubject = new BehaviorSubject<User>(JSON.parse(localStorage.getItem(JWT_USER)));
|
||||
this.checkStorage();
|
||||
this.currentUser = this.currentUserSubject.asObservable();
|
||||
}
|
||||
|
||||
checkStorage(): User {
|
||||
const existingToken: User = JSON.parse(localStorage.getItem(JWT_USER));
|
||||
if (existingToken === null || Date.now() > existingToken.exp * 1000) {
|
||||
localStorage.removeItem(JWT_USER);
|
||||
this.currentUserSubject = new BehaviorSubject<User>(null);
|
||||
return null;
|
||||
} else {
|
||||
this.currentUserSubject = new BehaviorSubject<User>(existingToken);
|
||||
return existingToken;
|
||||
}
|
||||
}
|
||||
|
||||
public get user(): User {
|
||||
const val = this.currentUserSubject.value;
|
||||
let val = this.currentUserSubject.value;
|
||||
if (val == null) {
|
||||
return val;
|
||||
}
|
||||
const expired = Date.now() > val.exp * 1000;
|
||||
let expired = Date.now() > val.exp * 1000;
|
||||
if (expired) {
|
||||
val = this.checkStorage();
|
||||
}
|
||||
if (val == null) {
|
||||
return null;
|
||||
}
|
||||
expired = Date.now() > val.exp * 1000;
|
||||
if (expired) {
|
||||
this.logout();
|
||||
return null;
|
||||
} else {
|
||||
return this.currentUserSubject.value;
|
||||
}
|
||||
return this.currentUserSubject.value;
|
||||
}
|
||||
|
||||
login(username: string, password: string, otp: string) {
|
||||
@ -73,10 +90,6 @@ export class AuthService {
|
||||
return Date.now() > (this.user.exp - (environment.ACCESS_TOKEN_REFRESH_MINUTES * 60)) * 1000;
|
||||
}
|
||||
|
||||
expired(): boolean {
|
||||
return Date.now() > this.user.exp * 1000;
|
||||
}
|
||||
|
||||
logout() {
|
||||
// remove user from local storage to log user out
|
||||
localStorage.removeItem(JWT_USER);
|
||||
|
||||
Reference in New Issue
Block a user