feat: implementa notificacao na rota de criar conta
This commit is contained in:
@@ -77,24 +77,34 @@
|
||||
import { toTypedSchema } from '@vee-validate/zod'
|
||||
import { z } from 'zod'
|
||||
|
||||
const { $toast } = useNuxtApp()
|
||||
|
||||
const toStr = (val) => (val == null ? '' : val)
|
||||
|
||||
const schema = toTypedSchema(
|
||||
z.object({
|
||||
email: z.preprocess(toStr, z.string().min(1, 'Email é obrigatório').pipe(z.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')),
|
||||
}),
|
||||
)
|
||||
|
||||
async function criarConta({ email, password }) {
|
||||
try {
|
||||
await fetch('/auth/register', {
|
||||
const res = await fetch('/auth/register', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ email, password }),
|
||||
})
|
||||
} catch (error) {
|
||||
window.alert('Ocorreu um erro ao criar a conta. Tente novamente mais tarde.')
|
||||
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}))
|
||||
$toast.error(data.message ?? 'Erro ao criar conta. Tente novamente.')
|
||||
return
|
||||
}
|
||||
|
||||
$toast.success('Conta criada com sucesso!')
|
||||
} catch {
|
||||
$toast.error('Erro ao criar conta. Tente novamente.')
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user