import { FormEvent, useState } from 'react' import axios from 'axios' import { useAuth, UserRole } from '../context/AuthContext' import { authService } from '../services/auth' import { logger } from '../utils/logger' import { decodeJwtPayload } from '../utils/jwt' import logoImg from '../assets/logo.png' // Ensure logo import is handled // Eye icon components for password visibility toggle const EyeIcon = () => ( ) const EyeSlashIcon = () => ( ) export function LoginPage() { const { login } = useAuth() const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [showPassword, setShowPassword] = useState(false) const [errorMessage, setErrorMessage] = useState(null) const [loading, setLoading] = useState(false) const [activeTab, setActiveTab] = useState<'login' | 'register'>('login') const resolveRole = (role?: string): UserRole => { logger.info('đ [Login] Resolving role:', role) switch (role?.toLowerCase()) { case 'admin': return 'admin' case 'dono': return 'owner' case 'colaborador': return 'employee' case 'entregador': return 'delivery' case 'customer': return 'customer' case 'seller': default: return 'seller' } } const onSubmit = async (event: FormEvent) => { event.preventDefault() setLoading(true) setErrorMessage(null) try { const response = await authService.login({ username, password }) const { token } = response if (!token) throw new Error('Resposta de login invĂĄlida. Verifique o usuĂĄrio e a senha.') const payload = decodeJwtPayload<{ role?: string, sub: string, company_id?: string }>(token) const role = resolveRole(payload?.role) login(token, role, username, payload?.sub || '', payload?.company_id, undefined, username) } catch (error) { const fallback = 'NĂŁo foi possĂvel autenticar. Verifique suas credenciais.' if (axios.isAxiosError(error)) { setErrorMessage(error.response?.data?.error ?? fallback) } else if (error instanceof Error) { setErrorMessage(error.message) } else { setErrorMessage(fallback) } } finally { setLoading(false) } } return ( {/* Blue Header with Logo */} SaveInMed Plataforma B2B de Medicamentos {/* Tabs */} setActiveTab('login')} className={`flex-1 rounded-lg py-2 text-sm font-medium transition-colors ${activeTab === 'login' ? 'bg-white text-blue-600 shadow-sm border border-gray-100' : 'text-gray-500 hover:text-gray-700' }`} > Entrar setActiveTab('register')} className={`flex-1 rounded-lg py-2 text-sm font-medium transition-colors ${activeTab === 'register' ? 'bg-white text-blue-600 shadow-sm border border-gray-100' : 'text-gray-500 hover:text-gray-700' }`} > Cadastrar {/* Login Form */} {activeTab === 'login' ? ( {errorMessage && ( Ops! NĂŁo encontramos esse login. Verifique seu e-mail/usuĂĄrio e senha. )} Email ou UsuĂĄrio @ setUsername(e.target.value)} /> Senha setPassword(e.target.value)} /> setShowPassword(!showPassword)} > {showPassword ? : } {loading ? ( <> Entrando... > ) : ( <> Entrar > )} ) : ( Funcionalidade de cadastro em breve. setActiveTab('login')} className="mt-4 text-blue-600 hover:underline text-sm" > Voltar para login )} ) }
Plataforma B2B de Medicamentos
Funcionalidade de cadastro em breve.