React + Firebase Authentication Error Handling (Handling account-exists-with-different-credential 错误)

React + Firebase Authentication Error Handling (Handling account-exists-with-different-credential Errors)

根据 https://firebase.google.com/docs/auth/web/google-signin#expandable-1 文档,当 error.code === 'auth/account-exists-with-different-credential' 时,signInWithPopUp() 将 return 一个 error.email。但是就我而言,出于某种原因,我没有。我收到以下错误:

FirebaseError: Firebase: Error (auth/account-exists-with-different-credential).
    at createErrorInternal (assert.ts:122:1)
    at _createError (assert.ts:83:1)
    at _makeTaggedError (index.ts:258:1)
    at _performFetchWithErrorHandling (index.ts:145:1)
    at async _performSignInRequest (index.ts:188:1)
    at async _signInWithCredential (credential.ts:37:1)
    at async PopupOperation.onAuthEvent (abstract_popup_redirect_operation.ts:102:1)

error.email 在我的例子中是未定义的。

代码:

const handleSignIn = (provider: AuthProvider) =>{
signInWithPopup(auth, provider).then((result) => {
  repeatAuth();
}).catch((error) => {
  console.log(error);
  if (error.code === 'auth/account-exists-with-different-credential') {
    let pendingCred = error.credential;
    let email = error.email;

    console.log(error.email); // undefined
    fetchSignInMethodsForEmail(auth, email).then( (methods) => {
      console.log(methods);
      if (methods[0] === 'password') {

        var password = promptUserForPassword(); // TODO: implement promptUserForPassword.

        signInWithEmailAndPassword(auth, email, password!).then((result) => {
          return linkWithCredential(result.user, pendingCred);
        }).then(() => {
          repeatAuth();
        });
        return;
      }
      // TODO: implement getProviderForProviderId.
      var provider = getProviderForProviderId(methods[0]);
      signInWithPopup(auth, provider).then((result) => {
        repeatAuth();
      });
    }).catch((error) => {
      console.log(error);
    });
  }
});

}

任何帮助都是有用的。谢谢

您可以按照以下步骤操作:-

  1. 从您的 firebase 转到控制台

  2. Select 你的项目

  3. 然后进入认证选项>sign-in方法

  4. 在高级部分您可以看到每个电子邮件地址一个帐户

  5. 然后将其更改为允许使用相同的电子邮件地址创建多个帐户

我也遇到了类似的问题。您可以使用

访问电子邮件

error.customData.email