简体   繁体   中英

Firebase auth password reset

I'm working on a firebase app that contains an authentication system that allows users to sign up/login and create a profile. The app stores and calls profile info about the users. The system worked just fine until I realized there's no way for a user to reset their password. So I added sendPasswordResetEmail function and it's been downhill since then. I'm a newbie to firebase and to StackOverflow so forgive the mess you're about to see. Am I missing something in that function?

const resetpassword = async () => {
const email = document.getElementById('email').value;
  try {
   const { user } = auth.sendPasswordResetEmail(email);
    alert('Password Reset Email Sent!');
    }
    catch(error) {
     console.log("error ===>", error);    
    if (error.message === "Firebase: Error (auth/user-not-found).") {
        alert("There is no user corresponding to this email address.")}
    else (errorCode == 'auth/invalid-email') {
      alert(errorMessage)} 
    }
 }

If you are using firebase version 9 then use code snippet below to reset user password

 import { getAuth, sendPasswordResetEmail } from "firebase/auth"; const auth = getAuth(); sendPasswordResetEmail(auth, email) .then(() => { // Password reset email sent! // .. }) .catch((error) => { const errorCode = error.code; const errorMessage = error.message; // .. });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM