sposchedule
95 строк · 2.5 Кб
1<script setup lang="ts">
2import { computed, reactive, ref, watch } from 'vue';
3import InputText from 'primevue/inputtext';
4import Password from 'primevue/password';
5import Button from 'primevue/button';
6import { useAuthStore } from '@/stores/auth';
7import { storeToRefs } from 'pinia';
8import router from '@/router';
9import LoadingBar from '../components/LoadingBar.vue';
10import { useDebounceFn } from '@vueuse/core';
11import Checkbox from 'primevue/checkbox';
12
13const authStore = useAuthStore();
14const { isAuth } = storeToRefs(authStore);
15const { login } = authStore;
16
17const credentials = reactive({
18email: '',
19password: '',
20remember: false,
21});
22
23watch(credentials, () => {
24if (error.value) {
25error.value = null;
26}
27});
28
29const error = ref();
30
31const isError = computed(() => Boolean(error.value));
32
33async function auth() {
34try {
35await login(credentials);
36} catch (e) {
37error.value = e?.response.data;
38
39return;
40}
41if (isAuth) router.push('/admin/schedules/changes');
42}
43
44const debouncedAuth = useDebounceFn(auth, 300);
45</script>
46
47<template>
48<LoadingBar />
49<div class="">
50<form
51class="flex flex-col justify-center items-center h-screen gap-4"
52@submit.prevent="debouncedAuth()"
53>
54<div
55class="flex flex-col gap-4 rounded-lg px-4 py-8 bg-surface-100 dark:bg-surface-900 max-w-72"
56>
57<h1 class="text-center dark:text-surface-100 text-2xl mb-4">
58Авторизация
59</h1>
60<InputText
61v-model="credentials.email"
62autofocus
63:invalid="isError"
64placeholder="Электронная почта"
65/>
66<Password
67v-model="credentials.password"
68:invalid="isError"
69fluid
70placeholder="Пароль"
71:feedback="false"
72toggle-mask
73/>
74<div class="flex gap-2 items-center">
75<Checkbox
76v-model="credentials.remember"
77input-id="remember"
78:binary="true"
79/>
80<label class="dark:text-surface-400 text-slate-800" for="remember">
81Запомнить меня
82</label>
83</div>
84<Button
85:disabled="!credentials.email || !credentials.password"
86type="submit"
87label="Войти"
88/>
89<span v-if="isError" class="text-red-400 w-full">{{
90error?.message
91}}</span>
92</div>
93</form>
94</div>
95</template>
96