Firebase JS API 身份验证 - account-exists-with-different-credential [英] Firebase JS API auth - account-exists-with-different-credential

查看:16
本文介绍了Firebase JS API 身份验证 - account-exists-with-different-credential的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在尝试解决这个问题时遇到了真正的问题,因此希望得到一些 Firebase 帮助/那些已经解决了同样问题的人.

We're having real problems trying to resolve this and so hoping for some Firebase assistance / those that have solved the same problem.

该应用程序是 React Native (0.43.2) 并使用 Firebase JS API(最新)

The app is React Native (0.43.2) and using Firebase JS API (latest)

我们提供 Facebook 和 Google 身份验证.工作正常.

We provide Facebook and Google auth. Works fine.

但是,如果用户:

  1. 使用 Facebook 登录(可以)
  2. 稍后,使用 Google 登录(也可以)
  3. 稍后,尝试使用 Facebook 登录 - BOOM!不太好,Firebase 返回此错误:

auth/account-exists-with-different-credential

通过阅读有关 SO 的文档和一些帖子,我们认为以下内容是正确的,但显然不是,因为我们得到了相同的身份验证错误.

From reading docs and a few posts on SO, we thought the following was correct but clearly not as we're getting the same auth error back.

...error returned by Firebase auth after trying Facebook login...

const email = error.email;
const pendingCred = error.credential;

firebase.auth().fetchProvidersForEmail(email)
.then(providers => {
   //providers returns this array -> ["google.com"]
   firebase.auth().signInWithCredential(pendingCred)
   .then(result => {
       result.user.link(pendingCred)
   })
   .catch(error => log(error))

对 signInWithCredential 的调用抛出相同的错误 auth/account-exists-with-different-credential.

The call to signInWithCredential is throwing the same error auth/account-exists-with-different-credential.

谁能帮忙指出我们在这个实现中做错了什么?非常感谢.

Can anyone help point out what we are doing wrong with this implementation? Greatly appreciated.

推荐答案

发生的事情是 Firebase 对所有电子邮件强制使用同一个帐户.由于您已经拥有用于同一电子邮件的 Google 帐户,因此您需要将该 Facebook 帐户链接到 Google 帐户,以便用户可以访问相同的数据,并且下次可以使用 Google 或 Facebook 登录同一帐户.

What is happening is that Firebase enforces a same account for all emails. As you already have a Google account for the same email, you need to link that Facebook account to the Google account so the user can access the same data and next time be able to sign in to the same account with either Google or Facebook.

您的代码段中的问题是您正在使用相同的凭据进行签名和链接.修改如下.当您收到错误auth/account-exists-with-different-credential"时,该错误将包含 error.email 和 error.credential(Facebook OAuth 凭据).您需要先查找 error.email 以获取现有提供程序.

The issue in your snippet is that you are signing and linking with the same credential. Modify as follows. When you get the error 'auth/account-exists-with-different-credential', the error will contain error.email and error.credential (Facebook OAuth credential). You need to first lookup the error.email to get the existing provider.

firebase.auth().fetchProvidersForEmail(error.email)
  .then(providers => {
    //providers returns this array -> ["google.com"]
    // You need to sign in the user to that google account
    // with the same email.
    // In a browser you can call:
    // var provider = new firebase.auth.GoogleAuthProvider();
    // provider.setCustomParameters({login_hint: error.email});
    // firebase.auth().signInWithPopup(provider)
    // If you have your own mechanism to get that token, you get it
    // for that Google email user and sign in
    firebase.auth().signInWithCredential(googleCred)
      .then(user => {
        // You can now link the pending credential from the first
        // error.
        user.linkWithCredential(error.credential)
      })
      .catch(error => log(error))

这篇关于Firebase JS API 身份验证 - account-exists-with-different-credential的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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