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); } }