在 firebase 和 Expo 中重置密码时如何检查当前密码?

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

我正在使用 react native expo 和 firebase 添加密码重置功能api。

第一次输入的是当前密码。

第二次输入新密码

第三个输入是确认新密码。

要查看当前密码,我必须获取当前密码。

请帮帮我!

您可以使用reauthenticateWithCredential() to verify user's password and then updatePassword()更新它:

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
  }
} 

更新密码需要最近一次登录 re-authentication 如果用户长时间登录,则有必要。