import { useState, type FormEvent } from 'react' import { Link, useLocation, useNavigate } from 'react-router-dom' import Navbar from '../components/Navbar' import { useAuth } from '../contexts/AuthContext' export default function LoginPage() { const { login } = useAuth() const navigate = useNavigate() const location = useLocation() const from = (location.state as { from?: { pathname: string } })?.from?.pathname || '/area-do-cliente' const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [error, setError] = useState('') const [loading, setLoading] = useState(false) async function handleSubmit(e: FormEvent) { e.preventDefault() setError('') setLoading(true) try { await login({ email, password }) navigate(from, { replace: true }) } catch (err: unknown) { const axiosErr = err as { response?: { status?: number } } if (axiosErr.response?.status === 401) { setError('E-mail ou senha incorretos.') } else { setError('Erro de conexão. Tente novamente.') } } finally { setLoading(false) } } const demoCredentials = [ { label: 'Admin', email: 'admin@demo.com', password: 'admin1234', admin: true }, { label: 'Usuário', email: 'usuario@demo.com', password: 'demo1234', admin: false }, ] function fillCredentials(cred: typeof demoCredentials[0]) { setEmail(cred.email) setPassword(cred.password) setError('') } return (
Acesse sua conta
Não tem conta?{' '} Cadastre-se
{/* Demo credentials */}Acesso de demonstração
{demoCredentials.map((cred) => ( ))}