|
| 1 | +<template> |
| 2 | + <GenericModal |
| 3 | + id="loginModal" |
| 4 | + :title="$t('loginModal.title')" |
| 5 | + size="sm" |
| 6 | + data-testid="login-modal" |
| 7 | + @open="open" |
| 8 | + @closed="closed" |
| 9 | + > |
| 10 | + <form v-if="modalVisible" @submit.prevent="login"> |
| 11 | + <p v-if="error" class="text-danger">{{ $t("loginModal.error") }}{{ error }}</p> |
| 12 | + |
| 13 | + <div class="mb-4"> |
| 14 | + <label for="loginPassword" class="col-form-label"> |
| 15 | + <div class="w-100"> |
| 16 | + <span class="label">{{ $t("loginModal.password") }}</span> |
| 17 | + </div> |
| 18 | + </label> |
| 19 | + <input |
| 20 | + id="loginPassword" |
| 21 | + v-model="password" |
| 22 | + ref="password" |
| 23 | + class="form-control" |
| 24 | + autocomplete="current-password" |
| 25 | + type="password" |
| 26 | + required |
| 27 | + /> |
| 28 | + </div> |
| 29 | + |
| 30 | + <button type="submit" class="btn btn-primary w-100 mb-3" :disabled="loading"> |
| 31 | + {{ $t("loginModal.login") }} |
| 32 | + </button> |
| 33 | + </form> |
| 34 | + </GenericModal> |
| 35 | +</template> |
| 36 | + |
| 37 | +<script> |
| 38 | +import GenericModal from "./GenericModal.vue"; |
| 39 | +import Modal from "bootstrap/js/dist/modal"; |
| 40 | +import api from "../api"; |
| 41 | +import { updateAuthStatus } from "../auth"; |
| 42 | +
|
| 43 | +export default { |
| 44 | + name: "LoginModal", |
| 45 | + components: { GenericModal }, |
| 46 | + data: () => { |
| 47 | + return { |
| 48 | + modalVisible: false, |
| 49 | + password: "", |
| 50 | + loading: false, |
| 51 | + error: "", |
| 52 | + }; |
| 53 | + }, |
| 54 | + methods: { |
| 55 | + open() { |
| 56 | + this.modalVisible = true; |
| 57 | + }, |
| 58 | + closed() { |
| 59 | + this.modalVisible = false; |
| 60 | + this.password = ""; |
| 61 | + this.loading = false; |
| 62 | + this.error = ""; |
| 63 | + }, |
| 64 | + focus() { |
| 65 | + console.log(this.$refs.password); |
| 66 | + this.$refs.password.focus(); |
| 67 | + }, |
| 68 | + closeModal() { |
| 69 | + Modal.getOrCreateInstance(document.getElementById("loginModal")).hide(); |
| 70 | + }, |
| 71 | + async login() { |
| 72 | + this.loading = true; |
| 73 | + this.error = ""; |
| 74 | +
|
| 75 | + try { |
| 76 | + const data = { password: this.password }; |
| 77 | + const res = await api.post("/auth/login", data, { |
| 78 | + validateStatus: (code) => [200, 401].includes(code), |
| 79 | + }); |
| 80 | + if (res.status === 200) { |
| 81 | + this.closeModal(); |
| 82 | + await updateAuthStatus(); |
| 83 | + this.$router.push({ path: "/config" }); |
| 84 | + this.password = ""; |
| 85 | + } |
| 86 | + if (res.status === 401) { |
| 87 | + this.error = this.$t("loginModal.invalid"); |
| 88 | + } |
| 89 | + } catch (err) { |
| 90 | + console.error(err); |
| 91 | + this.error = err.message; |
| 92 | + } finally { |
| 93 | + this.loading = false; |
| 94 | + } |
| 95 | + }, |
| 96 | + }, |
| 97 | +}; |
| 98 | +</script> |
0 commit comments