diff --git a/frontend/contexts/AuthContext.tsx b/frontend/contexts/AuthContext.tsx index 416226f..52f80fc 100644 --- a/frontend/contexts/AuthContext.tsx +++ b/frontend/contexts/AuthContext.tsx @@ -47,6 +47,28 @@ const AuthContext = createContext(undefined); export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => { const [user, setUser] = useState(null); + const getErrorMessage = (errorKey: string): string => { + // Map backend error messages to Portuguese + const errorMap: { [key: string]: string } = { + "email already registered": "Este e-mail já está cadastrado.", + "invalid credentials": "E-mail ou senha incorretos.", + "user not found": "Usuário não encontrado.", + "crypto/bcrypt: hashedPassword is not the hash of the given password": "Senha incorreta.", + "password is too short": "A senha é muito curta.", + "cpf already registered": "CPF já cadastrado.", + }; + + // Check for partial matches or exact matches + if (errorMap[errorKey]) return errorMap[errorKey]; + + // Fallbacks + if (errorKey.includes('hashedPassword')) return "Senha incorreta."; + if (errorKey.includes('email')) return "Erro relacionado ao e-mail."; + if (errorKey.includes('password')) return "Erro relacionado à senha."; + + return "Ocorreu um erro inesperado. Tente novamente."; + }; + const login = async (email: string, password?: string) => { // 1. Check for Demo/Mock users first const mockUser = MOCK_USERS.find(u => u.email === email); @@ -65,8 +87,8 @@ export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => }); if (!response.ok) { - console.error('Login failed:', response.statusText); - return false; + const errorData = await response.json().catch(() => ({})); + throw new Error(getErrorMessage(errorData.error || 'Falha no login')); } const data = await response.json(); @@ -107,7 +129,9 @@ export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => if (!response.ok) { const errorData = await response.json().catch(() => ({})); - throw new Error(errorData.error || 'Falha no cadastro'); + const rawError = errorData.error || 'Falha no cadastro'; + // Allow passing raw error if it's not in map, or map it + throw new Error(getErrorMessage(rawError)); } return true; diff --git a/frontend/pages/Login.tsx b/frontend/pages/Login.tsx index 2ea83b3..0b30f1e 100644 --- a/frontend/pages/Login.tsx +++ b/frontend/pages/Login.tsx @@ -22,9 +22,16 @@ export const Login: React.FC = ({ onNavigate }) => { setIsLoading(true); setError(''); - const success = await login(email, password); - if (!success) { - setError('Usuário não encontrado. Tente um dos e-mails de demonstração.'); + try { + const success = await login(email, password); + // If mock login returns true (it doesn't throw), we are good. + // If real login throws, we catch it below. + if (!success) { + // Fallback for mock if it returns false without throwing + setError('Credenciais inválidas, tente novamente ou tente um dos e-mails de demonstração.'); + } + } catch (err: any) { + setError(err.message || 'Erro ao realizar login'); } setIsLoading(false); }; diff --git a/frontend/pages/Register.tsx b/frontend/pages/Register.tsx index eeb0c01..8aa004b 100644 --- a/frontend/pages/Register.tsx +++ b/frontend/pages/Register.tsx @@ -305,6 +305,12 @@ export const Register: React.FC = ({ onNavigate }) => { + {error && !error.includes('termos') && !error.includes('senha') && !error.includes('coincidem') && ( +
+ {error} +
+ )} +