refac: aprimora logica de criar conta do usuario e padroniza requisicao com $fetch
This commit is contained in:
@@ -78,11 +78,13 @@
|
||||
import { toTypedSchema } from '@vee-validate/zod'
|
||||
import { z } from 'zod'
|
||||
|
||||
definePageMeta({ middleware: 'guest' })
|
||||
|
||||
const { $toast } = useNuxtApp()
|
||||
|
||||
const isLoading = ref(false)
|
||||
|
||||
const toStr = (val) => (val == null ? '' : val)
|
||||
const toStr = (val) => (val == null ? '' : String(val))
|
||||
|
||||
const schema = toTypedSchema(
|
||||
z.object({
|
||||
@@ -94,28 +96,16 @@ const schema = toTypedSchema(
|
||||
async function criarConta({ email, password }) {
|
||||
isLoading.value = true
|
||||
try {
|
||||
const res = await fetch('/auth/register', {
|
||||
await $fetch('/auth/register', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ email, password }),
|
||||
body: { email, password },
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
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,
|
||||
})
|
||||
$toast.success('Conta criada com sucesso!', { duration: 8000 })
|
||||
await navigateTo('/login')
|
||||
} catch {
|
||||
$toast.error('Erro ao criar conta. Tente novamente.', {
|
||||
duration: 8000,
|
||||
})
|
||||
} catch (error) {
|
||||
const message = error?.data?.statusMessage
|
||||
$toast.error(message ?? 'Erro ao criar conta. Tente novamente.', { duration: 8000 })
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user