import React, { useState } from 'react'; import { X, Loader2, GraduationCap, School, UserPlus } from 'lucide-react'; import { useAuth } from '../context/AuthContext'; import { useNavigate } from 'react-router-dom'; import { UserRole } from '../types'; interface LoginModalProps { isOpen: boolean; onClose: () => void; } export const LoginModal: React.FC = ({ isOpen, onClose }) => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [name, setName] = useState(''); const [role, setRole] = useState('student'); const [isRegistering, setIsRegistering] = useState(false); const [error, setError] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); const { login, register, logout } = useAuth(); const navigate = useNavigate(); if (!isOpen) return null; const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); setError(''); if (!email.trim() || !/^\S+@\S+\.\S+$/.test(email)) { setError('Informe um e-mail válido.'); return; } if (isRegistering && name.trim().length < 2) { setError('Informe seu nome.'); return; } if (password.length < 8) { setError('A senha deve ter pelo menos 8 caracteres.'); return; } setIsSubmitting(true); if (isRegistering && role === 'professor') { setIsSubmitting(false); setError('Contas de instrutor são liberadas pelo administrador da plataforma.'); return; } const signedInUser = isRegistering ? await register(name, email, password) : await login(email, password); setIsSubmitting(false); if (!signedInUser) { setError(isRegistering ? 'Não foi possível criar sua conta. Use um e-mail válido e uma senha com pelo menos 8 caracteres.' : 'Credenciais inválidas. Verifique seu e-mail e senha.'); return; } if (signedInUser.role !== 'superadmin' && signedInUser.role !== role) { logout(); setError(role === 'professor' ? 'Esta conta não possui acesso de instrutor.' : 'Esta conta possui acesso de instrutor. Selecione a opção Instrutor.'); return; } onClose(); navigate(signedInUser.role === 'superadmin' ? '/admin' : signedInUser.role === 'professor' ? '/gerenciar' : '/'); }; return (

Compor HUB ID

Acesse seu espaço de aprendizado e materiais

{isRegistering && setName(event.target.value)} className="w-full bg-white/10 text-white placeholder:text-white/30 px-4 py-3 rounded-[8px] focus:outline-none focus:ring-2 focus:ring-orange-500/50 focus:bg-white/15 transition-all text-body tracking-body" autoFocus />} setEmail(event.target.value)} className="w-full bg-white/10 text-white placeholder:text-white/30 px-4 py-3 rounded-[8px] focus:outline-none focus:ring-2 focus:ring-orange-500/50 focus:bg-white/15 transition-all text-body tracking-body" autoFocus={!isRegistering} /> setPassword(event.target.value)} className="w-full bg-white/10 text-white placeholder:text-white/30 px-4 py-3 rounded-[8px] focus:outline-none focus:ring-2 focus:ring-orange-500/50 focus:bg-white/15 transition-all text-body tracking-body" />
{error &&

{error}

}
); };