feat: update app version and build details; refactor auth service and components for improved functionality
This commit is contained in:
@@ -6,8 +6,8 @@ import { HttpClient } from '@angular/common/http';
|
||||
import { computed, inject, Injectable, signal } from '@angular/core';
|
||||
import { Router } from '@angular/router';
|
||||
import { CookieService } from 'ngx-cookie-service';
|
||||
import { BehaviorSubject, Observable, throwError } from 'rxjs';
|
||||
import { catchError, finalize, tap } from 'rxjs/operators';
|
||||
import { BehaviorSubject, from, Observable, throwError } from 'rxjs';
|
||||
import { catchError, finalize, switchMap, tap } from 'rxjs/operators';
|
||||
import config from 'src/config';
|
||||
import { COOKIE_KEYS, LOCAL_STORAGE_KEYS } from '../../../assets/constants';
|
||||
import {
|
||||
@@ -82,11 +82,14 @@ export class AuthService {
|
||||
},
|
||||
)
|
||||
.pipe(
|
||||
tap((response) => {
|
||||
this.handleAuthSuccess(response);
|
||||
this.refreshCookies();
|
||||
return response;
|
||||
}),
|
||||
switchMap((response) =>
|
||||
from(
|
||||
this.handleAuthSuccess(response).then(() => {
|
||||
this.refreshCookies();
|
||||
return response;
|
||||
}),
|
||||
),
|
||||
),
|
||||
catchError((error) => {
|
||||
console.error('Login error:', error);
|
||||
this.isLoadingSubject.next(false);
|
||||
@@ -98,19 +101,19 @@ export class AuthService {
|
||||
);
|
||||
}
|
||||
|
||||
logout(): void {
|
||||
async logout(): Promise<void> {
|
||||
localStorage.removeItem(LOCAL_STORAGE_KEYS.AUTH_TOKEN);
|
||||
localStorage.removeItem(LOCAL_STORAGE_KEYS.USER_DATA);
|
||||
localStorage.removeItem(LOCAL_STORAGE_KEYS.REFRESH_TOKEN);
|
||||
this.refreshCookies();
|
||||
await this.refreshCookies();
|
||||
|
||||
this.setCurrentUser(null);
|
||||
await this.setCurrentUser(null);
|
||||
this.router.navigate(['/auth']);
|
||||
}
|
||||
|
||||
refreshCookies() {
|
||||
this.cookieService.set(COOKIE_KEYS.POS_ID, '', new Date());
|
||||
this.cookieService.set(COOKIE_KEYS.ACCESS_TOKEN, '', new Date());
|
||||
async refreshCookies() {
|
||||
await this.cookieService.set(COOKIE_KEYS.POS_ID, '', new Date());
|
||||
await this.cookieService.set(COOKIE_KEYS.ACCESS_TOKEN, '', new Date());
|
||||
}
|
||||
|
||||
doRefreshToken(): Observable<IAuthResponse> {
|
||||
@@ -171,6 +174,22 @@ export class AuthService {
|
||||
return localStorage.getItem(LOCAL_STORAGE_KEYS.AUTH_TOKEN);
|
||||
}
|
||||
|
||||
waitForAuthReady(maxRetries = 20): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
let retries = 0;
|
||||
const check = () => {
|
||||
const isReady = Boolean(this.getToken()) && Boolean(this.currentAccount());
|
||||
if (isReady || retries >= maxRetries) {
|
||||
resolve(isReady);
|
||||
return;
|
||||
}
|
||||
retries += 1;
|
||||
setTimeout(check, 0);
|
||||
};
|
||||
check();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current refresh token
|
||||
*/
|
||||
@@ -247,7 +266,7 @@ export class AuthService {
|
||||
return true;
|
||||
}
|
||||
|
||||
private handleAuthSuccess(response: IAuthResponse): void {
|
||||
private async handleAuthSuccess(response: IAuthResponse): Promise<void> {
|
||||
localStorage.setItem(LOCAL_STORAGE_KEYS.AUTH_TOKEN, response.accessToken);
|
||||
this.token.set(response.accessToken);
|
||||
this.refreshToken.set(response.refreshToken);
|
||||
@@ -260,7 +279,7 @@ export class AuthService {
|
||||
// this.navigateByRole(response.user.role);
|
||||
}
|
||||
|
||||
private setCurrentUser(account: Maybe<IAuthAccountResponse>): void {
|
||||
private async setCurrentUser(account: Maybe<IAuthAccountResponse>): Promise<void> {
|
||||
this.currentAccount.set(account);
|
||||
this.currentAccountSubject.next(account);
|
||||
localStorage.setItem(LOCAL_STORAGE_KEYS.USER_DATA, JSON.stringify(account));
|
||||
|
||||
Reference in New Issue
Block a user