- Removed `window.__TEST_AUTH_STORE__` logic for E2E test store injection in `AuthProvider` and related comments. - Updated `AuthInitializer` to clarify E2E test behavior with mocked API responses. - Streamlined `AuthContext` handling by prioritizing explicit `store` prop or production singleton.
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
/**
|
|
* AuthInitializer Component
|
|
* Loads authentication state from storage on app initialization
|
|
* Must be a client component within the Providers tree
|
|
*/
|
|
|
|
'use client';
|
|
|
|
import { useEffect } from 'react';
|
|
import { useAuth } from '@/lib/auth/AuthContext';
|
|
|
|
/**
|
|
* AuthInitializer - Initializes auth state from encrypted storage on mount
|
|
*
|
|
* This component should be included in the app's Providers to ensure
|
|
* authentication state is restored from storage when the app loads.
|
|
*
|
|
* IMPORTANT: Uses useAuth() to respect dependency injection for testability.
|
|
* Do NOT import useAuthStore directly - it bypasses the Context wrapper.
|
|
*
|
|
* @example
|
|
* ```tsx
|
|
* // In app/providers.tsx
|
|
* export function Providers({ children }) {
|
|
* return (
|
|
* <QueryClientProvider>
|
|
* <AuthInitializer />
|
|
* {children}
|
|
* </QueryClientProvider>
|
|
* );
|
|
* }
|
|
* ```
|
|
*/
|
|
export function AuthInitializer() {
|
|
const loadAuthFromStorage = useAuth((state) => state.loadAuthFromStorage);
|
|
|
|
useEffect(() => {
|
|
// Load auth state from encrypted storage on mount
|
|
// E2E tests use the real flow with mocked API responses
|
|
loadAuthFromStorage();
|
|
}, [loadAuthFromStorage]);
|
|
|
|
// This component doesn't render anything
|
|
return null;
|
|
}
|