|
31 | 31 | @keypress.enter="tryLogin"
|
32 | 32 | ></v-text-field>
|
33 | 33 | <v-select
|
34 |
| - v-if="typesAccounts.length > 1" |
35 | 34 | v-model="type"
|
36 | 35 | class="type-select"
|
37 | 36 | :items="typesAccounts"
|
|
52 | 51 | </div>
|
53 | 52 | </template>
|
54 | 53 |
|
55 |
| -<script> |
56 |
| -import snackbar from "@/mixins/snackbar"; |
| 54 | +<script setup> |
| 55 | +import { computed, ref } from "vue"; |
| 56 | +import { useRouter } from "vue-router/composables"; |
| 57 | +import { useStore } from "../store"; |
| 58 | +
|
| 59 | +const loginFormRules = ref([(v) => !!v || "Required"]); |
| 60 | +const loginLoading = ref(false); |
| 61 | +const isLoginFailed = ref(false); |
| 62 | +const username = ref(""); |
| 63 | +const password = ref(""); |
| 64 | +const type = ref("Standard"); |
| 65 | +const typesAccounts = ref(["Standard", "WHMCS"]); |
| 66 | +
|
| 67 | +const router = useRouter(); |
| 68 | +const store = useStore(); |
| 69 | +
|
| 70 | +const tryLogin = async () => { |
| 71 | + loginLoading.value = true; |
| 72 | +
|
| 73 | + try { |
| 74 | + const { token } = await store.dispatch("auth/login", { |
| 75 | + login: username.value, |
| 76 | + password: password.value, |
| 77 | + type: type.value.toLowerCase(), |
| 78 | + }); |
| 79 | + store.commit("auth/setToken", ""); |
| 80 | +
|
| 81 | + await store.dispatch("namespaces/fetchById", 0); |
57 | 82 |
|
| 83 | + store.commit("auth/setToken", token); |
| 84 | +
|
| 85 | + await router.push({ name: "Home" }); |
| 86 | + store.dispatch("auth/fetchUserData"); |
| 87 | + } catch (error) { |
| 88 | + store.dispatch("auth/logout"); |
| 89 | + store.commit("snackbar/showSnackbarError", { |
| 90 | + message: error.response.data.message || "Error during login", |
| 91 | + }); |
| 92 | + if (error.response && error.response.status == 401) { |
| 93 | + isLoginFailed.value = true; |
| 94 | + } |
| 95 | + } finally { |
| 96 | + loginLoading.value = false; |
| 97 | + } |
| 98 | +}; |
| 99 | +
|
| 100 | +const getErrorMessages = computed(() => { |
| 101 | + return isLoginFailed.value ? ["username or password is not correct"] : []; |
| 102 | +}); |
| 103 | +</script> |
| 104 | + |
| 105 | +<script> |
58 | 106 | export default {
|
59 |
| - name: "login-view", |
60 |
| - mixins: [snackbar], |
61 |
| - data() { |
62 |
| - return { |
63 |
| - loginFormRules: [(v) => !!v || "Required"], |
64 |
| - loginLoading: false, |
65 |
| - isLoginFailed: false, |
66 |
| - username: "", |
67 |
| - password: "", |
68 |
| - type: "Standard", |
69 |
| - typesAccounts: ["Standard"], |
70 |
| - }; |
71 |
| - }, |
72 |
| - methods: { |
73 |
| - tryLogin() { |
74 |
| - this.loginLoading = true; |
75 |
| - (this.isLoginFailed = false), |
76 |
| - this.$store |
77 |
| - .dispatch("auth/login", { |
78 |
| - login: this.username, |
79 |
| - password: this.password, |
80 |
| - type: this.type.toLowerCase(), |
81 |
| - }) |
82 |
| - .then(() => { |
83 |
| - this.$router.push({ name: "Home" }); |
84 |
| - this.$store.dispatch("auth/fetchUserData"); |
85 |
| - }) |
86 |
| - .catch((error) => { |
87 |
| - console.log(error); |
88 |
| - this.showSnackbarError({ |
89 |
| - message: error.response.data.message || "Error during login", |
90 |
| - }); |
91 |
| - if (error.response && error.response.status == 401) { |
92 |
| - this.isLoginFailed = true; |
93 |
| - } |
94 |
| - }) |
95 |
| - .finally(() => { |
96 |
| - this.loginLoading = false; |
97 |
| - }); |
98 |
| - }, |
99 |
| - }, |
100 |
| - computed: { |
101 |
| - getErrorMessages() { |
102 |
| - return this.isLoginFailed ? ["username or password is not correct"] : []; |
103 |
| - }, |
104 |
| - }, |
| 107 | + name: "LoginView", |
105 | 108 | };
|
106 | 109 | </script>
|
107 | 110 |
|
|
0 commit comments