#28 fix(login): handle bad credentials.

This commit is contained in:
2026-02-21 13:38:00 -05:00
parent 4e79ecd56b
commit 0ba348cc64
2 changed files with 93 additions and 27 deletions

View File

@@ -1,26 +1,71 @@
<template>
<h1>Login</h1>
<v-form @submit.prevent="login">
<v-text-field v-model="username" label="Usuario" required />
<v-text-field v-model="password" label="Contraseña" type="password" required />
<v-btn type="submit">Entrar</v-btn>
<v-alert v-if="error" type="error">{{ error }}</v-alert>
<v-form ref="loginForm" @submit.prevent="onSubmit">
<v-text-field
v-model="username"
label="Usuario"
:rules="[requiredRule]"
required
/>
<v-text-field
v-model="password"
label="Contraseña"
type="password"
:rules="[requiredRule]"
required
/>
<v-btn type="submit" color="primary">Entrar</v-btn>
<v-alert v-if="error" type="error" class="mt-2">{{ error }}</v-alert>
</v-form>
</template>
<script setup>
import { ref } from 'vue';
<script>
import AuthService from '@/services/auth';
import { inject } from 'vue';
const username = ref('');
const password = ref('');
const error = ref('');
export default {
name: 'DonConfiao',
async function login() {
try {
await AuthService.login({ username: username.value, password: password.value });
} catch (e) {
error.value = e.message;
}
}
data() {
return {
username: '',
password: '',
error: '',
};
},
methods: {
requiredRule(value) {
return !!value || 'Este campo es obligatorio';
},
async onSubmit() {
this.error = '';
const form = this.$refs.loginForm;
const isValid = await form.validate();
if (!isValid) return;
if (!this.username || !this.password) {
this.error = 'Usuario y contraseña son obligatorios';
return;
}
try {
await AuthService.login({
username: this.username,
password: this.password,
});
this.$router.push({ path: '/' });
} catch (e) {
// Si el servicio devuelve un error (ej. 401) lo convertimos en excepción
const msg = e?.response?.data?.message ?? e.message;
this.error = msg ?? 'Error al iniciar sesión';
}
},
},
};
</script>