如果帐户已经存在并且与 Google 身份验证相关联,则 Facebook 的 Firebase 身份验证失败

Firebase Auth with Facebook fails if the account already exists and is associated with a Google Auth

我实现了一个包含 2 个按钮的简单应用程序:1 个通过 Google 登录,另一个通过 Facebook 登录。

两种身份验证方法都可以单独使用,但在以下情况下事情会变得更加复杂:

这是错误代码auth/account-exists-with-different-credential,这里已经在Whosebug中引用了

问题是我得到的 error 对象不包含 credential 属性 名称(顺便说一句,也不包含 email 属性 名称)在我构建的小应用程序中。

应用程序中 loginWithFacebook.js 的摘录

export async function loginWithFacebook() {
    signInWithPopup(auth, new FacebookAuthProvider())
        .then((result) => {
            const user = result.user;
            console.log(user);
        })
        .catch(function (error) {
            if (error.code === 'auth/account-exists-with-different-credential') {
                console.log(error.credential); // undefined
                console.log(error.email); // undefined
            }
        });
}

link to the testing app

我遇到了同样的问题,我通过查看返回错误的 customData 属性 解决了这个问题。这种不同的行为可能与 Firebase 的特定版本有关。我的是 firebase@9.6.9.

所以,试试看这是否有效:

export async function loginWithFacebook() {
signInWithPopup(auth, new FacebookAuthProvider())
    .then((result) => {
        const user = result.user;
        console.log(user);
    })
    .catch(function (error) {
        if (error.code === 'auth/account-exists-with-different-credential') {
            console.log(FacebookAuthProvider.credentialFromError(error));
            console.log(error.customData.email);
        }
    });

}