Firebase:Link 个现有用户的 Facebook 帐户

Firebase: Link facebook account with existing user

我在 Firebase 中有一个包含活跃用户的当前数据库,可以使用 user/pwd 登录,但现在我正在实施 facebook 登录,我意识到 link facebook 帐户的唯一方法是existing user 仅当用户已经使用 user/pwd 登录但不是登录之前。

我的应用程序中有两个按钮(使用 fb 和电子邮件登录)但是如果我尝试使用现有用户的同一电子邮件登录 fb,我将收到以下错误 auth/account-exists-with-different-credential 并且文档说为了解决这个问题,用户需要先登录然后 link.

您知道是否有办法 link 两个帐户但不首先执行登录,我的意思是,从登录视图?

您需要在 linking 之前先登录用户。如果您想确保它是同一个用户,这一点很重要。否则,您可以在 Firebase 控制台中为每封电子邮件切换到多个帐户。 解决办法,当你报错auth/account-exists-with-different-credential时,当你登录Facebook并且该账户已经作为密码账户存在时,报错会包含error.emailerror.credential。 然后调用 firebase.auth().fetchProvidersForEmail(error.email) 以解析该电子邮件的提供商 ID 列表。在这种情况下,它将包含 ['password']。然后您要求用户提供他们的密码。您调用 signInWithEmailAndPassword(error.email, password) 登录原始用户。然后你调用 firebase.auth().currentUser.linkWithCredential(error.credential) 到 link 密码帐户的 Facebook 凭据。现在两个帐户已合并,用户可以使用任一帐户登录。