FirebaseError:Firebase:使用 facebook 登录时出现错误(身份验证/缺少标识符)

问题描述 投票:0回答:1
try {
  const result = await signInWithPopup(auth, facebookProvider);
  const { user } = result;
} catch (error) {
  if (error.code === "auth/account-exists-with-different-credential") {
    try {
      // Check if the user has other providers linked to the same email
      const methods = await fetchSignInMethodsForEmail(auth, error.email);
      if (methods && methods.length > 0) {
        // User has other providers linked
        // Prompt user to sign in with the existing provider to link accounts
        const signInWithExistingProvider = async (provider) => {
          try {
            await signInWithRedirect(auth, provider);
            // Redirect will handle linking automatically
          } catch (linkError) {
            console.error("Error linking accounts:", linkError);
          }
        };

        // Example: Prompt user to sign in with Google
        // You can add more prompts for other providers if needed
        if (methods.includes("google.com")) {
          signInWithExistingProvider(googleProvider);
        }
      }
    } catch (linkError) {
      console.error("Error fetching sign-in methods:", linkError);
    }
  } else {
    console.log("Login Failed:", error);
  }
}

这就是我使用 firebase 处理 facebook 登录到我的 React 应用程序的方式。我在我的 React 应用程序中使用 Google 登录和 facebook 登录,我使用 firebase,并且在 firebase 身份验证内的登录方法中启用了这两个选项。正在发生的事情是: 如果用户第一次使用他的 facebook 登录,它工作正常,然后他注销,然后尝试使用 Google 登录方法,它也工作正常,然后他注销并再次尝试使用 firebase 登录,我收到错误:FirebaseError:Firebase:错误(auth/account-exists-with- different-credential)。为了解决这个问题,我尝试了上面的方法。 现在我遇到了错误:获取登录方法时出错:FirebaseError:Firebase:错误(auth/missing-identifier)。我的代码有什么问题吗?`

javascript reactjs firebase firebase-authentication oauth
1个回答
0
投票

请参阅 facebook-login#expandable-1

处理帐户存在不同凭证错误

如果您在 Firebase 控制台,当用户尝试登录提供程序(例如 例如 Facebook),其电子邮件已存在于另一个 Firebase 用户的提供商(例如 Google),错误 auth/account-exists-with- different-credential 与 AuthCredential 对象(Facebook 访问令牌)。完成登录 对于预期的提供商,用户必须首先签署现有的 提供商 (Google),然后链接到以前的 AuthCredential (Facebook 访问令牌)。弹出模式

如果你使用signInWithPopup,你可以处理 auth/account-exists-with- different-credential 错误,代码如下 下面的例子:

import {
  getAuth,
  linkWithCredential,
  signInWithPopup,
  FacebookAuthProvider,
} from "firebase/auth";

try {
  // Step 1: User tries to sign in using Facebook.
  let result = await signInWithPopup(getAuth(), new FacebookAuthProvider());
} catch (error) {
  // Step 2: User's email already exists.
  if (error.code === "auth/account-exists-with-different-credential") {
    // The pending Facebook credential.
    let pendingCred = error.credential;

    // Step 3: Save the pending credential in temporary storage,

    // Step 4: Let the user know that they already have an account
    // but with a different provider, and let them choose another
    // sign-in method.
  }
}

// ...

try {
  // Step 5: Sign the user in using their chosen method.
  let result = await signInWithPopup(getAuth(), userSelectedProvider);

  // Step 6: Link to the Facebook credential.
  // TODO: implement `retrievePendingCred` for your app.
  let pendingCred = retrievePendingCred();

  if (pendingCred !== null) {
    // As you have access to the pending credential, you can directly call the
    // link method.
    let user = await linkWithCredential(result.user, pendingCred);
  }

  // Step 7: Continue to app.
} catch (error) {
  // ...
}
© www.soinside.com 2019 - 2024. All rights reserved.