import React, { createContext, useContext, useEffect, useMemo, useState } from 'react'; import { onAuthStateChanged, User } from 'firebase/auth'; import { auth } from '@/shared/lib/firebase/config'; import { authService } from '@/features/auth/services/authService'; type AuthContextValue = { user: User | null; loading: boolean; logout: () => Promise; }; const AuthContext = createContext(undefined); type AuthProviderProps = { children: React.ReactNode; }; export function AuthProvider({ children }: AuthProviderProps) { const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { const unsubscribe = onAuthStateChanged(auth, (nextUser) => { setUser(nextUser); setLoading(false); }); return unsubscribe; }, []); const value = useMemo( () => ({ user, loading, logout: authService.logout, }), [user, loading] ); return {children}; } export function useAuthSession() { const context = useContext(AuthContext); if (!context) { throw new Error('useAuthSession must be used within AuthProvider'); } return context; }