sjbuysse
sjbuysse

Reputation: 4104

firebase credential missing in auth error catch

Logging in with firebase auth, I would like to link accounts with the same email address. I mean that when you log in with a facebook account that has the email address [email protected], and this email is already registered with a google account in your app, then I would like to link those facebook & google accounts.

For this I'm basing myself on the documentation: https://cloud.google.com/identity-platform/docs/link-accounts#handling_the_account-exists-with-different-credential_error

I'm using firebase v9 (beta), and don't get a credential property on my error.

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

The rest of the error object is filled in though (email, code, etc.)

Is this a breaking change with v9, or am I missing something?

EDIT, this is what the error looks like

{
  "code": "auth/email-already-in-use",
  "customData": {
    "appName": "[DEFAULT]",
    "email": "[email protected]",
    "_tokenResponse": {
      "federatedId": "http://facebook.com/2105242186275845",
      "providerId": "facebook.com",
      "email": "[email protected]",
      "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\":\"[email protected]\",\"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"
}

Upvotes: 1

Views: 1340

Answers (1)

samthecodingman
samthecodingman

Reputation: 26171

You have correctly identified that this will be a breaking change in v9.

In v9, the AuthError object will look like:

Property Type Description
appName string The name of the Firebase App which triggered this error.
email string | undefined The email of the user's account, used for sign-in/linking
phoneNumber string | undefined The phone number of the user's account, used for sign-in/linking
tenantId string | undefined The tenant ID being used for sign-in/linking
code string The Firebase error code identifying this error
message string An accessor for the friendly error message for this error code from prodErrorMap (simple messages) or debugErrorMap (verbose messages) as appropriate

Note: It's important to note that properties like email and phoneNumber are currently nested under the customData property, it is yet to be decided if accessors will be available to hoist them up to the top level or if you'll need to use error.customData.email and so on.

Importantly, you can see that v8's credential property is omitted. In v9, this property isn't parsed automatically and you are expected to handball it to OAuthProvider.credentialFromError() like so:

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
    });
  })

The documentation for this is yet to be updated and is being tracked as firebase-js-sdk Issue 5057.

Note: A Facebook user doesn't have to share their email with you, so expect it to be sometimes unavailable.

Upvotes: 1

Related Questions