首页 > 解决方案 > 电子邮件的 Firebase 身份验证检查已经存在,但不使用 signInWithEmailAndPassword

问题描述

我想检查firebase auth服务器中是否已经存在电子邮件,然后向用户询问密码。

有一个众所周知的解决方案是使用 signInWithEmailAndPassword 方法并依赖于错误消息。但在这种情况下,如果电子邮件不存在,它将创建一个帐户。我不想创建一个帐户,我只想知道电子​​邮件是否已经存在。

我应该为此使用云功能吗?我怎样才能做到这一点 ?

标签: firebase-authentication

解决方案


您可以使用fetchSignInMethodsForEmail. 这是一个使用 JavaScript 的示例:

firebase.auth().fetchSignInMethodsForEmail(email)
  .then((signInMethods) => {
    if (signInMethods.length) {
      // The email already exists in the Auth database. You can check the
      // sign-in methods associated with it by checking signInMethods array.
      // Show the option to sign in with that sign-in method.
    } else {
      // User does not exist. Ask user to sign up.
    }
  })
  .catch((error) => { 
    // Some error occurred.
  });

推荐阅读