fix: Add delay to login guard and more debug logging

This commit is contained in:
Mahi
2026-02-09 15:18:52 -04:00
parent 5b6f452b09
commit f3dd6b3dce

View File

@@ -93,6 +93,7 @@ export class AuthService implements OnDestroy {
private loadStoredUser(): void { private loadStoredUser(): void {
const token = this.storage.getToken(); const token = this.storage.getToken();
const user = this.storage.getUser<CurrentUserDto>(); const user = this.storage.getUser<CurrentUserDto>();
console.log('[AUTH DEBUG] loadStoredUser - token:', token ? 'EXISTS' : 'NULL', 'user:', user ? 'EXISTS' : 'NULL');
// Only restore state if we have both valid token AND user // Only restore state if we have both valid token AND user
if (token && user) { if (token && user) {
@@ -125,6 +126,7 @@ export class AuthService implements OnDestroy {
private setupStorageListener(): void { private setupStorageListener(): void {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
this.storageListener = (event: StorageEvent) => { this.storageListener = (event: StorageEvent) => {
console.log('[AUTH DEBUG] Storage event received, key:', event.key, 'isLoginInProgress:', this.isLoginInProgress);
// Skip if login is in progress to avoid race conditions // Skip if login is in progress to avoid race conditions
if (this.isLoginInProgress) { if (this.isLoginInProgress) {
console.log('[AUTH DEBUG] Skipping storage event during login'); console.log('[AUTH DEBUG] Skipping storage event during login');
@@ -276,7 +278,10 @@ export class AuthService implements OnDestroy {
this.storage.setUser(user); this.storage.setUser(user);
this.updateAuthState(user, true); this.updateAuthState(user, true);
} finally { } finally {
this.isLoginInProgress = false; // Delay clearing the login flag to allow async storage events to be ignored
setTimeout(() => {
this.isLoginInProgress = false;
}, 500);
this._isLoading.set(false); this._isLoading.set(false);
} }
} }