From 5b6f452b0974311ff5f7282494f127284039e5bf Mon Sep 17 00:00:00 2001 From: Mahi Date: Mon, 9 Feb 2026 15:04:28 -0400 Subject: [PATCH] fix: Prevent storage listener from clearing auth during login The storage event was firing when user was saved to localStorage, causing loadStoredUser() to run and clear the auth state. Added isLoginInProgress guard to skip storage events during login. --- frontend/src/app/core/services/auth.service.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/frontend/src/app/core/services/auth.service.ts b/frontend/src/app/core/services/auth.service.ts index 06c5d63..ba270f2 100644 --- a/frontend/src/app/core/services/auth.service.ts +++ b/frontend/src/app/core/services/auth.service.ts @@ -68,6 +68,9 @@ export class AuthService implements OnDestroy { // Track storage listener for cleanup private storageListener: ((event: StorageEvent) => void) | null = null; + // Guard to prevent storage listener from interfering during login + private isLoginInProgress = false; + constructor() { this.loadStoredUser(); this.setupStorageListener(); @@ -122,6 +125,12 @@ export class AuthService implements OnDestroy { private setupStorageListener(): void { if (typeof window !== 'undefined') { this.storageListener = (event: StorageEvent) => { + // Skip if login is in progress to avoid race conditions + if (this.isLoginInProgress) { + console.log('[AUTH DEBUG] Skipping storage event during login'); + return; + } + this.ngZone.run(() => { if (event.key === null || event.key?.includes('token') || event.key?.includes('user')) { // Storage was cleared or auth-related key changed @@ -210,6 +219,7 @@ export class AuthService implements OnDestroy { } this._isLoading.set(true); + this.isLoginInProgress = true; try { const response = await firstValueFrom( @@ -266,6 +276,7 @@ export class AuthService implements OnDestroy { this.storage.setUser(user); this.updateAuthState(user, true); } finally { + this.isLoginInProgress = false; this._isLoading.set(false); } }