Firebase 身份验证密码重置

问题描述 投票:0回答:2

我正在开发一个 Firebase 应用程序,其中包含一个身份验证系统,允许用户注册/登录并创建个人资料。该应用程序存储并调用有关用户的个人资料信息。系统运行得很好,直到我意识到用户无法重置密码。所以我添加了 sendPasswordResetEmail 函数,从那时起它就一直在走下坡路。我是 firebase 和 StackOverflow 的新手,所以请原谅您将要看到的混乱。我在那个函数中遗漏了什么吗?

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)} 
    }
 }
javascript firebase authentication firebase-authentication
2个回答
6
投票

如果您使用的是 Firebase 版本 9,请使用下面的代码片段重置用户密码

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;
    // ..
  });


0
投票

如果您想在应用程序内部处理此问题,这里给出了解决方案:https://stackoverflow.com/a/37955339/22991834

您需要在 Firebase 控制台内配置重置密码模板。

© www.soinside.com 2019 - 2024. All rights reserved.