diff --git a/src/components/dashboard/AtendimentosDia.tsx b/src/components/dashboard/AtendimentosDia.tsx index 68fd956..b3c7c5d 100644 --- a/src/components/dashboard/AtendimentosDia.tsx +++ b/src/components/dashboard/AtendimentosDia.tsx @@ -8,14 +8,23 @@ interface Atendimento { status: string; } +function isValidJWT(token?: string) { + return !!token && token.split('.').length === 3; +} + export const AtendimentosDia: React.FC = () => { - const { token } = useAuth(); + const { user, token } = useAuth(); const [atendimentos, setAtendimentos] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { async function fetchAtendimentos() { + if (!user || !isValidJWT(token)) { + setError('Usuário não autenticado ou token inválido. Faça login novamente.'); + setLoading(false); + return; + } try { setLoading(true); setError(null); @@ -35,7 +44,7 @@ export const AtendimentosDia: React.FC = () => { } } fetchAtendimentos(); - }, [token]); + }, [user, token]); if (loading) return
Carregando atendimentos...
; if (error) return
{error}
; diff --git a/src/components/dashboard/BenchmarkingEscritorio.tsx b/src/components/dashboard/BenchmarkingEscritorio.tsx index 3b8679d..4283b72 100644 --- a/src/components/dashboard/BenchmarkingEscritorio.tsx +++ b/src/components/dashboard/BenchmarkingEscritorio.tsx @@ -6,14 +6,23 @@ interface Benchmarking { emTramitacao: number; } +function isValidJWT(token?: string) { + return !!token && token.split('.').length === 3; +} + export const BenchmarkingEscritorio: React.FC = () => { - const { token } = useAuth(); + const { user, token } = useAuth(); const [benchmarking, setBenchmarking] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { async function fetchBenchmarking() { + if (!user || !isValidJWT(token)) { + setError('Usuário não autenticado ou token inválido. Faça login novamente.'); + setLoading(false); + return; + } try { setLoading(true); setError(null); @@ -33,7 +42,7 @@ export const BenchmarkingEscritorio: React.FC = () => { } } fetchBenchmarking(); - }, [token]); + }, [user, token]); if (loading) return
Carregando benchmarking...
; if (error) return
{error}
; diff --git a/src/components/dashboard/CalendarioAtendimentos.tsx b/src/components/dashboard/CalendarioAtendimentos.tsx index 96065d3..3703b4b 100644 --- a/src/components/dashboard/CalendarioAtendimentos.tsx +++ b/src/components/dashboard/CalendarioAtendimentos.tsx @@ -8,14 +8,23 @@ interface Evento { descricao: string; } +function isValidJWT(token?: string) { + return !!token && token.split('.').length === 3; +} + export const CalendarioAtendimentos: React.FC = () => { - const { token } = useAuth(); + const { user, token } = useAuth(); const [eventos, setEventos] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { async function fetchEventos() { + if (!user || !isValidJWT(token)) { + setError('Usuário não autenticado ou token inválido. Faça login novamente.'); + setLoading(false); + return; + } try { setLoading(true); setError(null); @@ -35,7 +44,7 @@ export const CalendarioAtendimentos: React.FC = () => { } } fetchEventos(); - }, [token]); + }, [user, token]); // Simulação de calendário mensal const diasMes = Array.from({ length: 30 }, (_, i) => i + 1);