'use client'; import { useEffect, useRef, useState } from 'react'; import { useAuthStore } from '../store/useAuthStore'; import { loginService } from '../services/login.service'; import { profileService } from '../../profile/services/profile.service'; import { mapToSafeProfile } from '../utils/mappers'; export function AuthInitializer({ children }: { children: React.ReactNode }) { const { setUser, logout } = useAuthStore(); const initialized = useRef(false); const [isChecking, setIsChecking] = useState(true); useEffect(() => { if (initialized.current) return; initialized.current = true; const validateSession = async () => { try { await loginService.refreshToken(); const profile = await profileService.getMe(); setUser(mapToSafeProfile(profile)); } catch (error) { console.warn('Sessão expirada ou inválida', error); logout(); } finally { setIsChecking(false); } }; validateSession(); }, [setUser, logout]); if (isChecking) { return (

Validando acesso...

); } return <>{children}; }