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

查看:27
本文介绍了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),我在 auth 设置中测试了 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 帐户:An具有相同电子邮件地址的帐户已存在".这两个我都不想发生

  • "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 后,我也收不到电子邮件(电子邮件 = 空).

"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 能否帮助我在许多应用中做我喜欢的事情(通过多种不同方式登录但结果相同)?

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天全站免登陆