Refactor to enforce AuthContext usage over useAuthStore and improve test stability
- Replaced `useAuthStore` with `useAuth` from `AuthContext` across frontend components and tests to ensure dependency injection compliance. - Enhanced E2E test stability by delaying navigation until the auth context is fully initialized. - Updated Playwright configuration to use a single worker to prevent mock conflicts. - Refactored test setup to consistently inject `AuthProvider` for improved isolation and mocking. - Adjusted comments and documentation to clarify dependency injection and testability patterns.
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect } from 'react';
|
||||
import { useAuthStore } from '@/lib/stores/authStore';
|
||||
import { useAuth } from '@/lib/auth/AuthContext';
|
||||
|
||||
/**
|
||||
* AuthInitializer - Initializes auth state from encrypted storage on mount
|
||||
@@ -15,6 +15,9 @@ import { useAuthStore } from '@/lib/stores/authStore';
|
||||
* 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
|
||||
@@ -29,9 +32,14 @@ import { useAuthStore } from '@/lib/stores/authStore';
|
||||
* ```
|
||||
*/
|
||||
export function AuthInitializer() {
|
||||
const loadAuthFromStorage = useAuthStore((state) => state.loadAuthFromStorage);
|
||||
const loadAuthFromStorage = useAuth((state) => state.loadAuthFromStorage);
|
||||
|
||||
useEffect(() => {
|
||||
// Skip loading from storage in E2E tests - test store is already injected
|
||||
if (typeof window !== 'undefined' && (window as any).__E2E_TEST__) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Load auth state from encrypted storage on mount
|
||||
loadAuthFromStorage();
|
||||
}, [loadAuthFromStorage]);
|
||||
|
||||
Reference in New Issue
Block a user