Firebase允许来自相同电子邮件地址的多个帐户

时间:2017-01-29 01:34:36

标签: firebase firebase-authentication

在Firebase控制台中,我专门将其设置为仅允许“每个电子邮件地址一个帐户”。这可以在“高级”下的登录方法选项卡中找到。

我有一个使用Google登录方法创建的帐户,其地址类似于“me@gmail.com”。如果我然后选择使用也使用“me@gmail.com”的帐户通过Facebook登录,Firebase允许它,但Users实体中的电子邮件地址为空。

Firebase文档说明:

  

如果您不允许多个帐户使用相同的电子邮件地址,请执行以下操作:   用户无法创建使用Google帐户登录的新帐户   如果已有帐户,请使用电子邮件地址ex@gmail.com   使用电子邮件地址ex@gmail.com和密码登录。

如果您尝试使用用户名/密码直接创建Firebase登录,而不是从Facebook和Google等两家提供商创建帐户,这是否只会计算在内?我的印象是,如果它找到重复的电子邮件地址,它应该拒绝注册/登录。我确实意识到引用声明“和密码”让我感到奇怪。

3 个答案:

答案 0 :(得分:2)

第1步:转到Firebase控制台>身份验证>登录方法。选中防止使用单个电子邮件ID创建多个帐户的选项。

步骤2:以下文档说明了如何使用自定义方法将多个提供程序连接到单个帐户。

https://firebase.google.com/docs/auth/web/account-linking

答案 1 :(得分:1)

转到Firebase控制台

在身份验证中 - >登录方法

向下滚动到高级部分 单击CHANGE,然后单击SAVE

enter image description here

答案 2 :(得分:0)

Firebase文档扩展了Kathir的回答,确实提供了solution

以下是从文档中复制的代码段。

// Step 1.
// User tries to sign in to Google.
auth.signInWithPopup(new firebase.auth.GoogleAuthProvider()).catch(function(error) {
  // An error happened.
  if (error.code === 'auth/account-exists-with-different-credential') {
    // Step 2.
    // User's email already exists.
    // The pending Google credential.
    var pendingCred = error.credential;

    // The provider account's email address.
    var email = error.email;

    // Get sign-in methods for this email.
    auth.fetchSignInMethodsForEmail(email).then(function(methods) {

      // Step 3.
      // If the user has several sign-in methods,
      // the first method in the list will be the "recommended" method to use.
      if (methods[0] === 'password') {

        // Asks the user their password.
        // In real scenario, you should handle this asynchronously.
        var password = promptUserForPassword(); // TODO: implement promptUserForPassword.

        auth.signInWithEmailAndPassword(email, password).then(function(user) {
          // Step 4a.
          return user.linkWithCredential(pendingCred);
        }).then(function() {
          // Google account successfully linked to the existing Firebase user.
          goToApp();
        });
        return;
      }

      // All the other cases are external providers.
      // Construct provider object for that provider.
      // TODO: implement getProviderForProviderId.
      var provider = getProviderForProviderId(methods[0]);

      // At this point, you should let the user know that he already has an account
      // but with a different provider, and let him validate the fact he wants to
      // sign in with this provider.
      // Sign in to provider. Note: browsers usually block popup triggered asynchronously,
      // so in real scenario you should ask the user to click on a "continue" button
      // that will trigger the signInWithPopup.
      auth.signInWithPopup(provider).then(function(result) {
        // Remember that the user may have signed in with an account that has a different email
        // address than the first one. This can happen as Firebase doesn't control the provider's
        // sign in flow and the user is free to login using whichever account he owns.
        // Step 4b.
        // Link to Google credential.
        // As we have access to the pending credential, we can directly call the link method.
        result.user.linkAndRetrieveDataWithCredential(pendingCred).then(function(usercred) {
          // Google account successfully linked to the existing Firebase user.
          goToApp();
        });
      });
    });
  }
});