refac: aprimora logica de criar conta do usuario e padroniza requisicao com $fetch

This commit is contained in:
2026-05-04 18:21:54 -05:00
parent b365fca7eb
commit 9b76618b71

View File

@@ -78,11 +78,13 @@
import { toTypedSchema } from '@vee-validate/zod' import { toTypedSchema } from '@vee-validate/zod'
import { z } from 'zod' import { z } from 'zod'
definePageMeta({ middleware: 'guest' })
const { $toast } = useNuxtApp() const { $toast } = useNuxtApp()
const isLoading = ref(false) const isLoading = ref(false)
const toStr = (val) => (val == null ? '' : val) const toStr = (val) => (val == null ? '' : String(val))
const schema = toTypedSchema( const schema = toTypedSchema(
z.object({ z.object({
@@ -94,28 +96,16 @@ const schema = toTypedSchema(
async function criarConta({ email, password }) { async function criarConta({ email, password }) {
isLoading.value = true isLoading.value = true
try { try {
const res = await fetch('/auth/register', { await $fetch('/auth/register', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, body: { email, password },
body: JSON.stringify({ email, password }),
}) })
if (!res.ok) { $toast.success('Conta criada com sucesso!', { duration: 8000 })
const data = await res.json().catch(() => ({}))
$toast.error(data.message ?? 'Erro ao criar conta. Tente novamente.', {
duration: 8000,
})
return
}
$toast.success('Conta criada com sucesso!', {
duration: 8000,
})
await navigateTo('/login') await navigateTo('/login')
} catch { } catch (error) {
$toast.error('Erro ao criar conta. Tente novamente.', { const message = error?.data?.statusMessage
duration: 8000, $toast.error(message ?? 'Erro ao criar conta. Tente novamente.', { duration: 8000 })
})
} finally { } finally {
isLoading.value = false isLoading.value = false
} }