簡體   English   中英

Firebase 如果帳戶已經存在並且與 Google Auth 關聯,則使用 Facebook 的 Auth 失敗

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

我實現了一個包含 2 個按鈕的簡單應用程序:1 個通過 Google 登錄,另一個通過 Facebook 登錄。

兩種身份驗證方法都可以單獨使用,但在以下情況下事情會變得更加復雜:

  • 用戶有一個 gmail 地址,並使用 Google 登錄服務進行第一次身份驗證
  • 然后,他回來並嘗試使用 Facebook 登錄服務登錄。

下面是錯誤代碼auth/account-exists-with-different-credential已經在 stackoverflow Firebase JS API auth - account-exists-with-different-credential中引用過

問題是我得到的error object 在我構建的小應用程序中不包含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
            }
        });
}

鏈接到測試應用程序

我有同樣的問題,我能夠通過查看返回錯誤的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);
        }
    });

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM