Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Password using react native and Firebase

In our project there is an option that the user can change their password. To do that

  1. Before change the password user must enter current password, then check whether the entered password is matched with the saved password right now.
  2. If it is OK, Then User has to update the old password (saved password) with new password.

I am using react native and Firebase for the developing of the project. If any one knows the solution, please let me know...

enter image description here

like image 679
Akila Nirangana Avatar asked Sep 06 '26 05:09

Akila Nirangana


1 Answers

Firebase Auth won't let you change a password unless you are recently authenticated. If you try to updatePassword without being recently authenticated, you will get an error auth/requires-recent-login. Here is how you can do it:

// Ask signed in user for current password.
const currentPass = window.prompt('Please enter current password');
const emailCred  = firebase.auth.EmailAuthProvider.credential(
    firebase.auth().currentUser, currentPass);
firebase.auth().currentUser.reauthenticateWithCredential(emailCred)
    .then(() => {
      // User successfully reauthenticated.
      const newPass = window.prompt('Please enter new password');
      return firebase.auth().currentUser.updatePassword(newPass);
    })
    .catch(error = > {
      // Handle error.
    });

Note the above example, uses window.prompt for illustration. You would use your own equivalent react-native UI here instead.

like image 100
bojeil Avatar answered Sep 07 '26 18:09

bojeil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!