Skip to content

Instantly share code, notes, and snippets.

@jlescalonap
Created December 20, 2023 20:25
Show Gist options
  • Save jlescalonap/5312efabd299621ec3fcdc7dcf7fc576 to your computer and use it in GitHub Desktop.
Save jlescalonap/5312efabd299621ec3fcdc7dcf7fc576 to your computer and use it in GitHub Desktop.
Función para realizar el registro del usuario.
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