Skip to content

Instantly share code, notes, and snippets.

@waldothedeveloper
Created April 1, 2022 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waldothedeveloper/639f7353713685594fe461af5fc36c5e to your computer and use it in GitHub Desktop.
Save waldothedeveloper/639f7353713685594fe461af5fc36c5e to your computer and use it in GitHub Desktop.
import { getAuth, sendPasswordResetEmail } from "firebase/auth";
import { useEffect, useState } from "react";
import { app } from "../../utils/firebaseConfig";
import { navigate } from "gatsby";
import { validEmailRegex } from "./validateEmailHelper";
const auth = getAuth(app);
auth.languageCode = `es`;
export const useForgotPassword = () => {
const [email, setEmail] = useState(``);
const [error, setError] = useState(null);
const [authErrors, setAuthErrors] = useState(``);
const [subMissionHasErrors, setSubmissionHasErrors] = useState(false);
useEffect(() => {
if (!validEmailRegex.test(email)) {
setError(`El correo electronico debe contener un formato valido`);
} else {
setError(``);
}
}, [email]);
const handleChange = (event) => {
const { value, name } = event.target;
if (name === `email`) {
setEmail(value);
}
};
const handleSubmit = (event) => {
if (event) event.preventDefault();
//
sendPasswordResetEmail(auth, email)
.then(() => {
// Password reset email sent!
navigate(`/email_sent_successfully`, { state: { email: email } });
})
.catch((error) => {
const { code } = error;
console.log(`code: `, code);
switch (code) {
case `auth/user-not-found`:
setAuthErrors(`El usuario o correo electronico no existe.`);
setSubmissionHasErrors(true);
break;
default:
setAuthErrors(
`Ha ocurrido un error inesperado. Intentelo de nuevo mas tarde.`
);
setSubmissionHasErrors(true);
break;
}
});
};
return {
handleSubmit,
error,
email,
handleChange,
authErrors,
subMissionHasErrors,
setSubmissionHasErrors,
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment