身份验证错误捕获中缺少 firebase 凭据

时间:2021-07-15 13:26:16

标签: javascript firebase firebase-authentication

使用 firebase auth 登录,我想关联具有相同电子邮件地址的帐户。我的意思是,当您使用电子邮件地址为 foo@gmail.com 的 facebook 帐户登录,并且此电子邮件已在您的应用程序中使用 google 帐户注册时,那么我想链接这些 facebook 和 google 帐户。

为此,我以文档为基础:https://cloud.google.com/identity-platform/docs/link-accounts#handling_the_account-exists-with-different-credential_error

我使用的是 firebase v9(测试版),但没有获得有关我的错误的凭据属性。

        getRedirectResult(auth).then((userCredential: UserCredential | null) => {
        // do stuff
        }).catch(error => {
            console.log(error.credential) //undefined
        })

尽管填写了错误对象的其余部分(电子邮件、代码等)

这是 v9 的重大更改,还是我遗漏了什么?

编辑, 这是错误的样子

{
  "code": "auth/email-already-in-use",
  "customData": {
    "appName": "[DEFAULT]",
    "email": "hidden@gmail.com",
    "_tokenResponse": {
      "federatedId": "http://facebook.com/2105242186275845",
      "providerId": "facebook.com",
      "email": "hidden@gmail.com",
      "emailVerified": false,
      "firstName": "hidden",
      "fullName": "hidden",
      "lastName": "hidden",
      "photoUrl": "https://graph.facebook.com/hidden/picture",
      "displayName": "hidden",
      "context": "",
      "oauthAccessToken": "hidden",
      "oauthExpireIn": 5100325,
      "rawUserInfo": "{\"name\":\"hidden\",\"last_name\":\"hidden\",\"granted_scopes\":[\"email\",\"public_profile\"],\"id\":\"2105242186275845\",\"first_name\":\"hidden\",\"email\":\"hidden@gmail.com\",\"picture\":{\"data\":{\"is_silhouette\":false,\"width\":100,\"url\":\"https://platform-lookaside.fbsbx.com/platform/profilepic/?asid\\hidden\\u0026height\\u003d100\\u0026width\\u003d100\\u0026ext\\u003d1629017191\\u0026hash\\u003dAeTs_VEMazok9d75eG4\",\"height\":100}}}",
      "errorMessage": "EMAIL_EXISTS",
      "kind": "identitytoolkit#VerifyAssertionResponse"
    }
  },
  "name": "FirebaseError"
}

1 个答案:

答案 0 :(得分:0)

您已正确识别这将是 v9 中的重大更改。

在 v9 中,AuthError object 将如下所示:

<头>
财产 类型 说明
appName string 触发此错误的 Firebase 应用的名称。
email string | undefined 用户账户的邮箱,用于登录/链接
phoneNumber string | undefined 用户帐号的电话号码,用于登录/链接
tenantId string | undefined 用于登录/链接的租户 ID
code string 标识此错误的 Firebase 错误代码
message string 此错误代码的友好错误消息的访问器来自 prodErrorMap(简单消息)或 debugErrorMap(详细消息),视情况而定

注意:重要的是要注意 emailphoneNumber 等属性当前嵌套在 customData 属性下,如果访问器将可以将它们提升到顶层,或者如果您需要使用 error.customData.email 等等。

重要的是,您可以看到 v8's credential property 被省略了。在 v9 中,此属性不会自动解析,您应该像这样手动将其传递给 OAuthProvider.credentialFromError()

getRedirectResult(auth)
  .then((userCredential: UserCredential | null) => {
    // do stuff
  })
  .catch(error => {
    const credential = OAuthProvider.credentialFromError(error);
    console.error("Auth failed with error: ", {
      code: error.code,
      email: error.email,
      credential
    });
  })

此方面的文档尚未更新,并被跟踪为 firebase-js-sdk Issue 5057

注意:Facebook 用户不必与您分享他们的电子邮件,因此预计它有时不可用。