Files
servicio_social_front/pages/index.vue
T
2021-12-07 10:40:29 -06:00

128 lines
3.1 KiB
Vue

<template>
<section
class="container is-flex is-justify-content-center is-align-items-center"
>
<form class="box">
<div class="has-text-centered">
<h1 class="is-size-1">IRIS</h1>
</div>
<b-field label="Usuario" :type="error">
<b-input @keyup.enter.native="login()" v-model="usuario" />
</b-field>
<b-field label="Contraseña" :type="error">
<b-input
type="password"
@keyup.enter.native="login()"
v-model="password"
password-reveal
/>
</b-field>
<div class="has-text-centered">
<b-button
type="is-success"
:disabled="error || !(usuario && password)"
@click="login()"
>
Iniciar Sesión
</b-button>
</div>
</form>
<b-loading :is-full-page="true" :can-cancel="false" v-model="isLoading" />
</section>
</template>
<script>
import axios from 'axios'
export default {
data() {
return {
isLoading: false,
error: '',
password: '',
usuario: '',
}
},
methods: {
login() {
if (this.usuario && this.password && !this.error) {
const data = {
usuario: this.usuario,
password: this.password,
}
this.isLoading = true
axios
.post(`${process.env.api}/usuario/login`, data)
.then((res) => {
const info = res.data
localStorage.setItem('token', info.token)
localStorage.setItem('idUsuario', info.Usuario.idUsuario)
localStorage.setItem('usuario', info.Usuario.usuario)
localStorage.setItem('nombre', info.Usuario.nombre)
localStorage.setItem(
'idTipoUsuario',
info.Usuario.TipoUsuario.idTipoUsuario
)
localStorage.setItem(
'tipoUsuario',
info.Usuario.TipoUsuario.tipoUsuario
)
this.isLoading = false
this.$router.push(`/${info.Usuario.TipoUsuario.tipoUsuario}`)
})
.catch((err) => {
this.isLoading = false
this.error = 'is-danger'
this.$buefy.dialog.alert({
title: 'Error al iniciar sesión',
message: err.response.data.message,
type: 'is-danger',
hasIcon: true,
icon: 'alert-circle',
ariaRole: 'alertdialog',
ariaModal: true,
})
})
}
},
},
watch: {
password() {
if (this.error) this.error = ''
},
usuario() {
if (this.error) this.error = ''
},
},
created() {
if (
localStorage.getItem('token') &&
localStorage.getItem('idUsuario') &&
localStorage.getItem('usuario') &&
localStorage.getItem('nombre') &&
localStorage.getItem('idTipoUsuario') &&
localStorage.getItem('tipoUsuario')
)
this.$router.push(`/${localStorage.getItem('tipoUsuario')}`)
else localStorage.clear()
},
layout: 'login',
}
</script>
<style scoped>
section {
height: 75vh;
}
form {
width: 30rem;
}
</style>