'use client'; import { Box, CircularProgress, Typography } from '@mui/material'; 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.obterColaboradorAtual(); setUser(mapToSafeProfile(profile)); } catch (error) { console.warn('Sessão expirada ou inválida', error); logout(); } finally { setIsChecking(false); } }; validateSession(); }, [setUser, logout]); if (isChecking) { return ( theme.palette.grey[200], }} size={48} thickness={4} value={100} /> theme.palette.primary.main, animationDuration: '550ms', position: 'absolute', left: 0, [`& .MuiCircularProgress-circle`]: { strokeLinecap: 'round', }, }} size={48} thickness={4} /> Validando acesso... ); } return <>{children}; }