Files
pcpuma_unam_usuario/components/Login.vue
T
2022-07-12 10:43:19 -05:00

137 lines
2.6 KiB
Vue

<template>
<div class="full-h is-flex is-justify-content-center is-align-items-center">
<form class="px-4">
<div class="columns is-centered is-mobile is-gapless">
<div class="column is-8">
<b-image
class="image is-3by1"
:src="require('@/assets/icon.png')"
alt="pcpuma_logo"
/>
</div>
</div>
<b-field label="Número de Cuenta/Trabajador" :type="error">
<b-input
type="text"
maxlength="9"
:has-counter="false"
@keyup.enter.native="login()"
v-model="usuario"
rounded
/>
</b-field>
<b-field label="Contraseña" :type="error">
<b-input
type="password"
@keyup.enter.native="login()"
v-model="password"
password-reveal
rounded
/>
</b-field>
<p class="has-text-white">
¿No tienes cuenta?
<b class="registrate" @click="$router.push(`/Registro`)">Regístrate</b>
</p>
<b-field class="has-text-centered pt-5">
<b-button
type="is-success"
@click="login()"
:disabled="error || !usuario || !password"
>
Iniciar Sesión
</b-button>
</b-field>
</form>
</div>
</template>
<script>
import axios from 'axios'
export default {
props: {
imprimirError: { type: Function, required: true },
updateIsLoading: { type: Function, required: true },
},
data() {
return {
error: '',
password: '',
usuario: '',
}
},
methods: {
login() {
if (this.usuario && this.password && !this.error) {
const data = {
usuario: this.usuario,
password: this.password,
}
this.updateIsLoading(true)
axios
.post(`${process.env.api}/auth/login-usuario`, data)
.then((res) => {
localStorage.setItem('usuario', JSON.stringify(res.data.usuario))
localStorage.setItem('token', res.data.token)
this.updateIsLoading(false)
this.$router.push(`/Home`)
})
.catch((err) => {
this.error = 'is-danger'
this.updateIsLoading(false)
this.imprimirError(err.response.data)
})
}
},
},
watch: {
password() {
if (this.error) this.error = ''
},
usuario() {
if (this.error) this.error = ''
},
}
}
</script>
<style scoped>
.full-h {
height: 80vh;
}
.registrate:hover {
cursor: pointer;
color: #ccc;
text-decoration: underline;
}
form {
width: 30rem;
}
</style>