mirror of
https://github.com/lucasrcsantana/story-generator.git
synced 2025-12-17 05:47:52 +00:00
- Atualiza lógica de redirecionamento no LoginForm - Ajusta verificação de roles no useAuth hook - Melhora proteção de rotas no ProtectedRoute - Atualiza rotas para suportar diferentes perfis de usuário
168 lines
5.4 KiB
TypeScript
168 lines
5.4 KiB
TypeScript
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<void>;
|
|
onRegisterClick?: () => void;
|
|
}
|
|
|
|
const userTypeIcons = {
|
|
school: <School className="h-8 w-8 text-purple-600" />,
|
|
teacher: <GraduationCap className="h-8 w-8 text-purple-600" />,
|
|
student: <User className="h-8 w-8 text-purple-600" />
|
|
};
|
|
|
|
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;
|
|
|
|
const userRole = data.user?.user_metadata?.role;
|
|
|
|
switch (userRole) {
|
|
case 'student':
|
|
navigate('/aluno');
|
|
break;
|
|
case 'teacher':
|
|
navigate('/professor');
|
|
break;
|
|
case 'school':
|
|
navigate('/dashboard');
|
|
break;
|
|
default:
|
|
throw new Error('Tipo de usuário inválido');
|
|
}
|
|
|
|
} catch (err) {
|
|
console.error('Erro no login:', err);
|
|
setError('Email ou senha incorretos');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="min-h-screen bg-gradient-to-b from-purple-50 to-white py-12">
|
|
<div className="max-w-md mx-auto px-4">
|
|
<div className="text-center mb-8">
|
|
<div className="inline-flex items-center justify-center w-16 h-16 rounded-full bg-purple-100 mb-4">
|
|
{userTypeIcons[userType]}
|
|
</div>
|
|
<h1 className="text-3xl font-bold text-gray-900 mb-2">
|
|
Bem-vindo de volta!
|
|
</h1>
|
|
<p className="text-gray-600">
|
|
Faça login como {userTypeLabels[userType]}
|
|
</p>
|
|
</div>
|
|
|
|
{error && (
|
|
<div className="mb-6 p-4 bg-red-50 border border-red-200 rounded-lg text-red-600">
|
|
{error}
|
|
</div>
|
|
)}
|
|
|
|
<div className="bg-white rounded-2xl shadow-xl p-8">
|
|
<form onSubmit={handleSubmit} className="space-y-6">
|
|
<div>
|
|
<label htmlFor="email" className="block text-sm font-medium text-gray-700">
|
|
Email
|
|
</label>
|
|
<input
|
|
id="email"
|
|
type="email"
|
|
required
|
|
value={email}
|
|
onChange={(e) => 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"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="password" className="block text-sm font-medium text-gray-700">
|
|
Senha
|
|
</label>
|
|
<div className="relative mt-1">
|
|
<input
|
|
id="password"
|
|
type={showPassword ? 'text' : 'password'}
|
|
required
|
|
value={password}
|
|
onChange={(e) => 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"
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowPassword(!showPassword)}
|
|
className="absolute inset-y-0 right-0 pr-3 flex items-center"
|
|
>
|
|
{showPassword ? (
|
|
<EyeOff className="h-5 w-5 text-gray-400" />
|
|
) : (
|
|
<Eye className="h-5 w-5 text-gray-400" />
|
|
)}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<button
|
|
type="submit"
|
|
disabled={loading}
|
|
className="w-full flex justify-center items-center gap-2 py-3 px-4 border border-transparent rounded-lg shadow-sm text-white bg-purple-600 hover:bg-purple-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-purple-500 disabled:opacity-50"
|
|
>
|
|
{loading ? (
|
|
'Entrando...'
|
|
) : (
|
|
<>
|
|
<LogIn className="h-5 w-5" />
|
|
Entrar
|
|
</>
|
|
)}
|
|
</button>
|
|
</form>
|
|
|
|
{onRegisterClick && (
|
|
<div className="mt-6 text-center">
|
|
<p className="text-sm text-gray-600">
|
|
Ainda não tem uma conta?{' '}
|
|
<button
|
|
onClick={onRegisterClick}
|
|
className="text-purple-600 hover:text-purple-500 font-medium"
|
|
>
|
|
Cadastre-se
|
|
</button>
|
|
</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
} |