import React, { useState } from 'react'; import { LogIn, Eye, EyeOff, School, GraduationCap, User } from 'lucide-react'; import { useAuth } from '../../hooks/useAuth'; import { useNavigate } from 'react-router-dom'; import { supabase } from '../../lib/supabase'; interface LoginFormProps { userType: 'school' | 'teacher' | 'student'; onLogin?: (credentials: { email: string; password: string }) => Promise; onRegisterClick?: () => void; } const userTypeIcons = { school: , teacher: , student: }; const userTypeLabels = { school: 'Escola', teacher: 'Professor', student: 'Aluno' }; export function LoginForm({ userType, onLogin, onRegisterClick }: LoginFormProps) { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); const { signIn } = useAuth(); const navigate = useNavigate(); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); setLoading(true); try { const { data, error } = await supabase.auth.signInWithPassword({ email: email, password: password }); if (error) throw error; if (!data.user) { throw new Error('Usuário não encontrado'); } const userRole = data.user.user_metadata?.role; if (userRole !== userType) { throw new Error(`Este não é um login de ${userTypeLabels[userType]}`); } switch (userType) { case 'school': navigate('/dashboard'); break; case 'teacher': navigate('/professor'); break; case 'student': navigate('/aluno'); break; default: throw new Error('Tipo de usuário inválido'); } } catch (err) { console.error('Erro no login:', err); if (err instanceof Error) { setError(err.message); } else { setError('Email ou senha incorretos'); } } finally { setLoading(false); } }; return (
{userTypeIcons[userType]}

Bem-vindo de volta!

Faça login como {userTypeLabels[userType]}

{error && (
{error}
)}
setEmail(e.target.value)} className="mt-1 block w-full rounded-lg border-gray-300 shadow-sm focus:border-purple-500 focus:ring-purple-500" />
setPassword(e.target.value)} className="block w-full rounded-lg border-gray-300 shadow-sm focus:border-purple-500 focus:ring-purple-500 pr-10" />
{onRegisterClick && (

Ainda não tem uma conta?{' '}

)}
); }