forked from cardosofelipe/fast-next-template
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.
This commit is contained in:
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useRouter, usePathname } from 'next/navigation';
|
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 { useMe } from '@/lib/api/hooks/useAuth';
|
||||||
import { AuthLoadingSkeleton } from '@/components/layout';
|
import { AuthLoadingSkeleton } from '@/components/layout';
|
||||||
import config from '@/config/app.config';
|
import config from '@/config/app.config';
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { usePathname } from 'next/navigation';
|
import { usePathname } from 'next/navigation';
|
||||||
import { useAuth } from '@/lib/stores';
|
import { useAuth } from '@/lib/auth/AuthContext';
|
||||||
import { useLogout } from '@/lib/api/hooks/useAuth';
|
import { useLogout } from '@/lib/api/hooks/useAuth';
|
||||||
import {
|
import {
|
||||||
DropdownMenu,
|
DropdownMenu,
|
||||||
|
|||||||
@@ -28,11 +28,20 @@ let refreshPromise: Promise<string> | null = null;
|
|||||||
/**
|
/**
|
||||||
* Auth store accessor
|
* Auth store accessor
|
||||||
* Dynamically imported to avoid circular dependencies
|
* 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
|
* Note: Tested via E2E tests when interceptors are invoked
|
||||||
*/
|
*/
|
||||||
/* istanbul ignore next */
|
/* istanbul ignore next */
|
||||||
const getAuthStore = async () => {
|
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');
|
const { useAuthStore } = await import('@/lib/stores/authStore');
|
||||||
return useAuthStore.getState();
|
return useAuthStore.getState();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user