From 26d43ff9e16525985ecdd5683d1dbed4aceae105 Mon Sep 17 00:00:00 2001 From: Felipe Cardoso Date: Tue, 4 Nov 2025 00:01:33 +0100 Subject: [PATCH] Refactor `useAuth` imports to utilize `AuthContext` and enhance test store injection handling - Replaced `useAuthStore` imports with `useAuth` from `AuthContext` in `AuthGuard` and `Header` for consistency. - Enhanced `getAuthStore` to prioritize E2E test store injection for improved testability. - Updated comments to reflect changes and clarify usage patterns. --- frontend/src/components/auth/AuthGuard.tsx | 2 +- frontend/src/components/layout/Header.tsx | 2 +- frontend/src/lib/api/client.ts | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/auth/AuthGuard.tsx b/frontend/src/components/auth/AuthGuard.tsx index 1e9bc9f..71a5dd8 100644 --- a/frontend/src/components/auth/AuthGuard.tsx +++ b/frontend/src/components/auth/AuthGuard.tsx @@ -8,7 +8,7 @@ import { useEffect, useState } from 'react'; import { useRouter, usePathname } from 'next/navigation'; -import { useAuth } from '@/lib/stores'; +import { useAuth } from '@/lib/auth/AuthContext'; import { useMe } from '@/lib/api/hooks/useAuth'; import { AuthLoadingSkeleton } from '@/components/layout'; import config from '@/config/app.config'; diff --git a/frontend/src/components/layout/Header.tsx b/frontend/src/components/layout/Header.tsx index bf1a773..bca9972 100644 --- a/frontend/src/components/layout/Header.tsx +++ b/frontend/src/components/layout/Header.tsx @@ -8,7 +8,7 @@ import Link from 'next/link'; import { usePathname } from 'next/navigation'; -import { useAuth } from '@/lib/stores'; +import { useAuth } from '@/lib/auth/AuthContext'; import { useLogout } from '@/lib/api/hooks/useAuth'; import { DropdownMenu, diff --git a/frontend/src/lib/api/client.ts b/frontend/src/lib/api/client.ts index ea596da..31cab9d 100644 --- a/frontend/src/lib/api/client.ts +++ b/frontend/src/lib/api/client.ts @@ -28,11 +28,20 @@ let refreshPromise: Promise | null = null; /** * Auth store accessor * Dynamically imported to avoid circular dependencies + * Checks for E2E test store injection before using production store * * Note: Tested via E2E tests when interceptors are invoked */ /* istanbul ignore next */ const getAuthStore = async () => { + // Check for E2E test store injection (same pattern as AuthProvider) + if (typeof window !== 'undefined' && (window as any).__TEST_AUTH_STORE__) { + const testStore = (window as any).__TEST_AUTH_STORE__; + // Test store must have getState() method for non-React contexts + return testStore.getState(); + } + + // Production: use real Zustand store const { useAuthStore } = await import('@/lib/stores/authStore'); return useAuthStore.getState(); };