Created
December 20, 2023 20:25
-
-
Save jlescalonap/5312efabd299621ec3fcdc7dcf7fc576 to your computer and use it in GitHub Desktop.
Función para realizar el registro del usuario.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const handleSubmit = async (e: any) => { | |
e.preventDefault(); | |
const username = e.target[0].value; | |
const email = e.target[1].value; | |
const password = e.target[2].value; | |
if (!isValidUsername(username)) { | |
setError('Nombre de usuário inválido'); | |
return; | |
} | |
if (!isValidEmail(email)) { | |
setError('Email inválido'); | |
return; | |
} | |
if (!password || password.length < 8) { | |
setError('Password inválido'); | |
return; | |
} | |
try { | |
const res = await fetch('/api/register', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify({ | |
username, | |
email, | |
password, | |
}), | |
}); | |
await signIn('credentials', { email, password, callbackUrl: '/dashboard' }) | |
if (res.status === 400) { | |
setError('Este e-mail já esta en uso, por favor intente otro.'); | |
} | |
} catch (error) { | |
setError('Error, try again'); | |
console.log(error); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment