login funcionando
This commit is contained in:
@@ -34,7 +34,7 @@
|
|||||||
</h2>
|
</h2>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Form :validation-schema="schema" @submit="criarConta" class="grid gap-5">
|
<Form :validation-schema="schema" @submit="entrarConta" class="grid gap-5">
|
||||||
<div class="grid gap-2">
|
<div class="grid gap-2">
|
||||||
<label class="m-0 text-xs font-semibold uppercase leading-[1.4] tracking-[0.96px] text-[#777169]"
|
<label class="m-0 text-xs font-semibold uppercase leading-[1.4] tracking-[0.96px] text-[#777169]"
|
||||||
for="email">
|
for="email">
|
||||||
@@ -86,9 +86,30 @@
|
|||||||
const schema = toTypedSchema(
|
const schema = toTypedSchema(
|
||||||
z.object({
|
z.object({
|
||||||
email: z.preprocess(toStr, z.string().min(1, 'Email é obrigatório').email('Email inválido')),
|
email: z.preprocess(toStr, z.string().min(1, 'Email é obrigatório').email('Email inválido')),
|
||||||
password: z.preprocess(toStr, z.string().min(8, 'A senha deve ter no mínimo 8 caracteres')),
|
password: z.preprocess(toStr, z.string().min(1, 'A senha é obrigatória')),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async function entrarConta({ email, password }) {
|
||||||
|
isLoading.value = true
|
||||||
|
try {
|
||||||
|
const res = await fetch('/auth/login', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ email, password }),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const data = await res.json().catch(() => ({}))
|
||||||
|
$toast.error(data.message ?? 'Erro para efetuar o login. Tente novamente.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
$toast.success('Login realizado com sucesso!')
|
||||||
|
} catch {
|
||||||
|
$toast.error('Erro para efetuar o login. Tente novamente.')
|
||||||
|
} finally {
|
||||||
|
isLoading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -326,7 +326,7 @@ export async function handleLogin(event: H3Event) {
|
|||||||
const user = await prisma.user.findUnique({ where: { email } })
|
const user = await prisma.user.findUnique({ where: { email } })
|
||||||
|
|
||||||
if (!user || !verifyPassword(password, user.passwordHash)) {
|
if (!user || !verifyPassword(password, user.passwordHash)) {
|
||||||
throw createError({ statusCode: 401, statusMessage: 'Invalid credentials' })
|
throw createError({ statusCode: 401, statusMessage: 'Credenciais inválidas!' })
|
||||||
}
|
}
|
||||||
|
|
||||||
const accessToken = await signAccessToken(event, { sub: user.id })
|
const accessToken = await signAccessToken(event, { sub: user.id })
|
||||||
|
|||||||
Reference in New Issue
Block a user