pcpuma_unam_operador/components/admin/Login.vue

121 lines
3.0 KiB
Vue
Raw Normal View History

2022-06-27 21:59:35 +00:00
<template>
2022-09-14 17:06:09 +00:00
<div class="full-h is-flex is-justify-content-center is-align-items-center">
<form class="box">
<div class="has-text-centered">
<h2 class="is-size-1">PC Puma</h2>
</div>
2022-06-27 21:59:35 +00:00
2022-09-14 17:06:09 +00:00
<b-field label="Usuario" :type="error">
<b-input
autocomplete="off"
v-model="usuario"
@keyup.enter.native="login()"
/>
</b-field>
2022-06-27 21:59:35 +00:00
2022-09-14 17:06:09 +00:00
<b-field label="Contraseña" :type="error">
<b-input
autocomplete="off"
type="password"
@keyup.enter.native="login()"
v-model="password"
password-reveal
/>
</b-field>
2022-06-27 21:59:35 +00:00
2022-12-05 15:36:30 +00:00
<div class="is-flex is-justify-content-center my-3">
2022-09-14 17:06:09 +00:00
<recaptcha />
2022-12-05 15:36:30 +00:00
</div>
2022-08-09 20:14:51 +00:00
2022-09-14 17:06:09 +00:00
<BotonIniciarSesion
:login="login"
2022-10-05 10:00:59 +00:00
:disabled="error || !usuario || !password ? true : false"
2022-09-14 17:06:09 +00:00
/>
</form>
</div>
2022-06-27 21:59:35 +00:00
</template>
<script>
import axios from 'axios'
2022-09-14 17:06:09 +00:00
import jwt_decode from 'jwt-decode'
import BotonIniciarSesion from '@/components/botones/BotonIniciarSesion'
2022-06-27 21:59:35 +00:00
export default {
2022-09-14 17:06:09 +00:00
components: { BotonIniciarSesion },
2023-01-17 17:44:41 +00:00
props: {
updateIsLoading: { type: Function, required: true, default: () => {} },
},
2022-06-27 21:59:35 +00:00
data() {
2023-01-17 17:44:41 +00:00
return { error: '', usuario: '', password: '' }
2022-06-27 21:59:35 +00:00
},
methods: {
2022-08-09 20:14:51 +00:00
async login() {
2022-12-05 15:36:30 +00:00
if (this.usuario && this.password && !this.error) {
const token = await this.$recaptcha.getResponse().catch((err) => {
this.$alertsGenericos.imprimirError(this.$buefy, this.$router, {
message: 'Favor de completar el recaptcha.',
})
2022-08-10 00:13:45 +00:00
})
2022-08-09 20:14:51 +00:00
2022-12-05 15:36:30 +00:00
if (token) {
const data = {
operador: this.usuario,
password: this.password,
}
2022-12-05 15:36:30 +00:00
this.updateIsLoading(true)
return axios
.post(`${process.env.api}/auth/login-admin`, data, {
headers: { recaptcha: token },
})
.then(async (res) => {
const token = res.data.token
const operador = jwt_decode(token).Operador
2022-07-29 19:34:21 +00:00
2022-12-05 15:36:30 +00:00
localStorage.setItem('token', token)
await this.$recaptcha.reset()
this.updateIsLoading(false)
if (operador.tipoUsuario.id_tipo_usuario === 2)
this.$router.push(
'/admin/configuracion/instituciones/buscar_institucion'
)
else this.$router.push('/prestamo_devolucion')
})
.catch(async (err) => {
this.error = 'is-danger'
await this.$recaptcha.reset()
this.updateIsLoading(false)
this.$alertsGenericos.imprimirError(
this.$buefy,
this.$router,
err.response.data
2022-07-29 14:16:12 +00:00
)
2022-12-05 15:36:30 +00:00
})
}
2022-06-27 21:59:35 +00:00
}
},
},
watch: {
password() {
if (this.error) this.error = ''
},
usuario() {
if (this.error) this.error = ''
},
},
2022-08-09 20:14:51 +00:00
beforeDestroy() {
this.$recaptcha.destroy()
},
2022-06-27 21:59:35 +00:00
}
</script>
<style scoped>
2022-09-14 17:06:09 +00:00
.full-h {
min-height: 80vh;
}
2022-06-27 21:59:35 +00:00
form {
2022-09-14 17:06:09 +00:00
width: 25rem;
2022-06-27 21:59:35 +00:00
}
</style>