Firebase,通过同一电子邮件不同的提供商登录 [英] Firebase, login by same email different provider

查看:67
本文介绍了Firebase,通过同一电子邮件不同的提供商登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的用户可以使用许多不同的提供程序登录,但是如果他们仅使用一个电子邮件地址,他们将获得相同的结果.例如,在stackoverflow中,我可以通过Facebook,Google登录...但是我仍然可以保留我的个人资料以及我的帖子...

I want my users can login using many different provider but they will get same result if they use only one email address. For example, in stackoverflow I can login by Facebook, Google ... but I still can keep my profile as well as my posts ...

例如,在Firebase Web中,如果我的用户使用电子邮件/密码提供者创建了一个帐户,则他的email ="ex@gmail.com" password ="123456".此帐户具有uid ="account1",我使用此uid作为密钥在Firebase数据库中存储有关他的其他信息.

In Firebase Web, for example if my user created an account with email/password provider, his email="ex@gmail.com" password="123456". This account has uid="account1" and I use this uid as a key to store additional information about him in Firebase Database.

有一天,他选择通过Google提供者和Facebook提供者(仍为ex@gmail.com)登录,我在身份验证设置中测试了2种情况:

Once day, he choose login by Google provider and Facebook provider (still ex@gmail.com), I test with 2 cases in auth setting:

  • 防止创建具有相同电子邮件地址的多个帐户":新的Google登录名将覆盖旧的"account1",由于以下错误,我无法使用"ex@gmail.com"创建新的Facebook帐户:帐户已经存在具有相同的电子邮件地址".我都不想发生

  • "Prevent creation of multiple accounts with the same email address": new Google login will override old "account1" and I can not create new Facebook account with "ex@gmail.com" due to error: "An account already exists with the same email address". Which both I don't want to happend

允许创建具有相同电子邮件地址的多个帐户":使用此选项,我可以创建具有相同电子邮件地址的多个帐户,但是它们具有不同的uid,并且我不知道如何将这些uid链接到"account1" ?在Google和Facebook登录后,我也无法收到电子邮件(email = null).

"Allow creation of multiple accounts with the same email address": with this option I can create many account with same email address but they have diffrent uid and I don't know how to link these uid to "account1"? I also can't get email (email = null) after login by Google and Facebook.

Firebase可以帮助我在许多App中做我喜欢的事情吗(以多种不同方式登录但结果相同)?

So can firebase help me do the thing that I love in many App (login by many different ways but same result)?

推荐答案

Firebase Auth通过防止创建具有相同电子邮件地址的多个帐户"设置对此提供支持.但是,Google是一种特殊情况,因为它是经过验证的提供商,并且将使用同一封电子邮件覆盖未验证的帐户.例如,如果创建了一个电子邮件/密码帐户并且未对其进行验证,然后某个用户使用同一封电子邮件登录Google,则旧密码将被覆盖并取消链接,但是会返回同一用户(相同的uid).

This is supported by Firebase Auth through the "Prevent creation of multiple accounts with the same email address" setting. However, Google is a special case as it is a verified provider and will overwrite unverified accounts with the same email. For example, if an email/password account is created and not verified and then a user signs in with Google with the same email, the old password is overridden and unlinked but the same user (same uid) is returned.

在其他情况下,这就是处理方式.

For other cases, this is how it is handled.

假设您使用电子邮件/密码通过帐户user@example.com使用电子邮件/密码登录.然后,用户尝试使用同一封电子邮件登录Facebook提供程序. Auth后端将引发错误,并且需要链接.完成之后,用户可以使用两个帐户登录.

Let's say you sign in with Email/Password using an email/password with account user@example.com. A user then tries to sign in with a Facebook provider using the same email. The Auth backend will throw an error and linking will be required. After that is done, a user can sign in with either accounts.

这里是一个例子:

var existingEmail = null;
var pendingCred = null;
var facebookProvider = new firebase.auth.FacebookAuthProvider();
firebase.auth().signInWithPopup(facebookProvider)
  .then(function(result) {
    // Successful sign-in.
  });
  .catch(function(error) {
    // Account exists with different credential. To recover both accounts
    // have to be linked but the user must prove ownership of the original
    // account.
    if (error.code == 'auth/account-exists-with-different-credential') {
      existingEmail = error.email;
      pendingCred = error.credential;
      // Lookup existing account’s provider ID.
      return firebase.auth().fetchProvidersForEmail(error.email)
        .then(function(providers) {
           if (providers.indexOf(firebase.auth.EmailAuthProvider.PROVIDER_ID) != -1) {
             // Password account already exists with the same email.
             // Ask user to provide password associated with that account.
             var password = window.prompt('Please provide the password for ' + existingEmail);
             return firebase.auth().signInWithEmailAndPassword(existingEmail, password);    
           } else if (providers.indexOf(firebase.auth.GoogleAuthProvider.PROVIDER_ID) != -1) {
             var googProvider = new firebase.auth.GoogleAuthProvider();
             // Sign in user to Google with same account.
             provider.setCustomParameters({'login_hint': existingEmail});
             return firebase.auth().signInWithPopup(googProvider).then(function(result) {
               return result.user;
             });
           } else {
             ...
           }
        })
        .then(function(user) {
          // Existing email/password or Google user signed in.
          // Link Facebook OAuth credential to existing account.
          return user.linkWithCredential(pendingCred);
        });
    }
    throw error;
  });

这篇关于Firebase,通过同一电子邮件不同的提供商登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆