'use client'; import { authClient } from '@/lib/auth-client'; import { useRouter } from 'next/navigation'; import { useEffect, useState } from 'react'; export default function AuthCallback() { const [isValidating, setIsValidating] = useState(true); const [error, setError] = useState(''); const router = useRouter(); useEffect(() => { const validateUser = async () => { try { // Aguardar um pouco para garantir que a sessão foi criada await new Promise((resolve) => setTimeout(resolve, 1000)); const session = await authClient.getSession(); if (!session?.data?.user?.email) { setError('Erro ao obter dados do usuário'); setIsValidating(false); return; } // Validar se o email é permitido const response = await fetch('/api/validate-email', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ email: session.data.user.email, }), }); const data = await response.json(); if (!data.isAllowed) { // Se o email não for permitido, fazer logout e mostrar erro await authClient.signOut(); setError(data.message); setIsValidating(false); return; } // Se o email for permitido, redirecionar para o dashboard router.push('/dashboard'); } catch (error) { console.error('Erro na validação:', error); setError('Erro interno do servidor'); setIsValidating(false); } }; validateUser(); }, [router]); if (isValidating) { return (

Validando acesso...

); } if (error) { return (

Acesso Negado

{error}

); } return null; }