Skip to content
Advertisement

How to check current password when reset password in firebase and Expo? [closed]

I’m adding password reset function using react native expo and firebase api.

UI link

First input is current password.

Second input is new password.

And third input is confirm new password.

To check the current password, I have to get current password.

Please help me!

Advertisement

Answer

You can use reauthenticateWithCredential() to verify user’s password and then updatePassword() to update it:

import { getAuth, reauthenticateWithCredential, EmailAuthProvider } from "firebase/auth";

const auth = getAuth();

const resetUserPassword = async () => {
  const user = auth.currentUser;

  const cred = EmailAuthProvider.credential(user.email, "[USER_PASSWORD]");

  try {
    await reauthenticateWithCredential(user, cred)
   
    // User entered correct credentials
    // Update password
    await updatePassword(auth.currentUser, "[NEW_PASSWORD]");
  } catch (e) {
    console.log(e.code, e.message)
    // Could be incorrect credentials
  }
} 

Updating password requires a recent login re-authentication is necessary if user is logged in for long time.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement