Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)} 
    }
 }
like image 800
BillTheCheetah Avatar asked Jan 22 '26 22:01

BillTheCheetah


1 Answers

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;
    // ..
  });
like image 121
Prasenjeet Symon Avatar answered Jan 25 '26 12:01

Prasenjeet Symon