Files
yt2mp3/app/pages/login.vue
2026-01-15 01:35:44 +00:00

109 lines
2.4 KiB
Vue

<script setup lang="ts">
import * as z from 'zod'
import type { FormSubmitEvent, AuthFormField } from '@nuxt/ui'
const toast = useToast()
const wrong_login = ref(false)
const fields: AuthFormField[] = [{
name: 'email',
type: 'email',
label: 'Email',
placeholder: 'Enter your email',
required: true
}, {
name: 'password',
label: 'Password',
type: 'password',
placeholder: 'Enter your password',
required: true
} , {
name: 'remember',
label: 'Remember me',
type: 'checkbox'
}]
const schema = z.object({
email: z.email('Invalid email'),
password: z.string('Password is required').min(8, 'Must be at least 8 characters')
})
type Schema = z.output<typeof schema>
function onSubmit(payload: FormSubmitEvent<Schema>) {
return fetch("/login", {
method: "POST",
headers: {
"Content-Type" : "application/json"
},
body: JSON.stringify({
email: payload.data.email,
password: payload.data.password
})
}).then((response) => {
if (response.ok)
window.location.href = "/";
else if (response.status == 401) {
wrong_login.value = true;
// show for 5 seconds
setTimeout(() => wrong_login.value = false, 5000);
}
})
}
</script>
<template>
<div class="flex flex-col items-center justify-center gap-4 p-4 mt-7">
<UPageCard class="w-full max-w-md dark:bg-zinc-950 rounded-3xl">
<UAuthForm
:schema="schema"
title="Login"
icon="solar:shield-user-broken"
:fields="fields"
:submit="{
label: 'Login',
class: 'rounded-full',
variant: 'soft',
loadingAuto: true
}"
@submit="onSubmit"
>
<template #validation>
<Transition name="slide-fade">
<UAlert v-if="wrong_login"
color="error"
icon="lucide:circle-x"
variant="soft"
:ui="{ icon: 'self-center' }"
title="Incorrect Login"
description="Either your e-mail or your password are wrong."/>
</Transition>
</template>
</UAuthForm>
</UPageCard>
</div>
</template>
<style>
.slide-fade-enter-from {
opacity: 0;
transform: translateY(-10px);
}
.slide-fade-enter-active {
transition: opacity 0.3s, transform 0.3s;
}
.slide-fade-leave-to {
opacity: 0;
transform: translateY(-10px);
}
.slide-fade-leave-active {
transition: opacity 0.3s, transform 0.3s;
}
</style>