| 1 | <template> |
| 2 | <div> |
| 3 | <n-form ref="formRef" :model :rules :disabled="loading"> |
| 4 | <n-form-item path="username" label="Username"> |
| 5 | <n-input |
| 6 | v-model:value="model.username" |
| 7 | placeholder="Insert your Username" |
| 8 | :input-props="{ autocomplete: 'username' }" |
| 9 | size="large" |
| 10 | @keydown.enter="signIn" |
| 11 | /> |
| 12 | </n-form-item> |
| 13 | <n-form-item path="password" label="Password"> |
| 14 | <n-input |
| 15 | v-model:value="model.password" |
| 16 | type="password" |
| 17 | show-password-on="click" |
| 18 | placeholder="Insert your password" |
| 19 | :input-props="{ autocomplete: 'password' }" |
| 20 | size="large" |
| 21 | @keydown.enter="signIn" |
| 22 | /> |
| 23 | </n-form-item> |
| 24 | <div class="flex flex-col items-end gap-6"> |
| 25 | <div class="w-full"> |
| 26 | <n-button type="primary" class="w-full!" size="large" :loading :disabled="!isValid" @click="signIn"> |
| 27 | Sign in |
| 28 | </n-button> |
| 29 | </div> |
| 30 | </div> |
| 31 | </n-form> |
| 32 | </div> |
| 33 | </template> |
| 34 | |
| 35 | <script lang="ts" setup> |
| 36 | import type { FormInst, FormRules, FormValidationError } from "naive-ui" |
| 37 | import type { LoginPayload } from "@/api/endpoints/auth" |
| 38 | import { NButton, NForm, NFormItem, NInput, useMessage } from "naive-ui" |
| 39 | import { computed, ref, watch } from "vue" |
| 40 | import { useRouter } from "vue-router" |
| 41 | import { useAuthStore } from "@/stores/auth" |
| 42 | |
| 43 | interface ModelType { |
| 44 | username: string | null |
| 45 | password: string | null |
| 46 | } |
| 47 | |
| 48 | const loading = ref(false) |
| 49 | const router = useRouter() |
| 50 | const formRef = ref<FormInst | null>(null) |
| 51 | const message = useMessage() |
| 52 | const model = ref<ModelType>({ |
| 53 | username: null, |
| 54 | password: null |
| 55 | }) |
| 56 | const authStore = useAuthStore() |
| 57 | |
| 58 | const rules: FormRules = { |
| 59 | username: [ |
| 60 | { |
| 61 | required: true, |
| 62 | trigger: ["blur"], |
| 63 | message: "Username is required" |
| 64 | } |
| 65 | ], |
| 66 | password: [ |
| 67 | { |
| 68 | required: true, |
| 69 | trigger: ["blur"], |
| 70 | message: "Password is required" |
| 71 | } |
| 72 | ] |
| 73 | } |
| 74 | |
| 75 | const isValid = computed(() => { |
| 76 | return model.value.username && model.value.password |
| 77 | }) |
| 78 | |
| 79 | function signIn(e: Event) { |
| 80 | e.preventDefault() |
| 81 | formRef.value?.validate((errors: Array<FormValidationError> | undefined) => { |
| 82 | if (!errors) { |
| 83 | loading.value = true |
| 84 | |
| 85 | const payload: LoginPayload = { |
| 86 | username: model.value.username || "", |
| 87 | password: model.value.password || "" |
| 88 | } |
| 89 | |
| 90 | authStore |
| 91 | .login(payload) |
| 92 | .then(() => { |
| 93 | router.push({ path: "/", replace: true }) |
| 94 | }) |
| 95 | .catch(err => { |
| 96 | message.error(err?.message || "An error occurred. Please try again later.") |
| 97 | }) |
| 98 | .finally(() => { |
| 99 | loading.value = false |
| 100 | }) |
| 101 | } else { |
| 102 | message.error("Invalid credentials") |
| 103 | } |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | watch(isValid, val => { |
| 108 | if (val) { |
| 109 | formRef.value?.validate() |
| 110 | } |
| 111 | }) |
| 112 | </script> |