firebase-admin-node icon indicating copy to clipboard operation
firebase-admin-node copied to clipboard

getUsers(identifiers) returns the same user in .notFound and in .users

Open LeOndaz opened this issue 4 years ago • 12 comments

Hello. It's a very strange thing to happen but here's the code:

const customToken = await admin.auth().getUsers([userIdentifier])
  .then(async (result) => {
    if (result.notFound[0]) { // not found by email or phoneNumber
      Logger.error('User not found.');
      newMerchantCreated = true;
      Logger.info('creating a new User');
      return await createUser(userIdentifier);
    }
    return getCustomToken(result.users[0].uid);
  });

inspecting result.notFound and result.users, both have the same user. The user identifier obj is:

{
    "phoneNumber": "valid phone number of an existing user"
}

response:

info: {
  "users": [
    {
      "uid": "2wI3MEQFLVftAVp4TstEdDsp4Xd2",
      "emailVerified": false,
      "phoneNumber": "+201142494391",
      "disabled": false,
      "metadata": {
        "lastSignInTime": "Tue, 17 Aug 2021 22:56:48 GMT",
        "creationTime": "Fri, 06 Aug 2021 06:52:28 GMT"
      },
      "tokensValidAfterTime": "Fri, 06 Aug 2021 06:52:28 GMT",
      "providerData": [
        {
          "uid": "+201142494391",
          "providerId": "phone",
          "phoneNumber": "+201142494391"
        }
      ]
    }
  ],
  "notFound": [
    {
      "phoneNumber": "+20-1142494391"
    }
  ]
} 

I'm not sure if this is a bug, but I'm also sure that this should not happen.

LeOndaz avatar Aug 21 '21 00:08 LeOndaz

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

google-oss-bot avatar Aug 21 '21 00:08 google-oss-bot

Can you share the full JSON response of the function call? Something like this:

const response = await admin.auth().getUsers([userIdentifier]);
console.log(JSON.stringify(response));

hiranya911 avatar Aug 21 '21 00:08 hiranya911

@hiranya911 added

LeOndaz avatar Aug 21 '21 00:08 LeOndaz

Oh! This is an interesting one :)

The phone number in the query is given as +20-1142494391. But it's recorded as +201142494391 in the backend (notice the missing hyphen). So when the SDK tries to calculate the notFound list locally they essentially get treated as 2 different entries (i.e. uid.phoneNumber !== userRecord.phoneNumber). I'm not quite sure what to do about this. @bojeil-google @rsgowman any thoughts?

@LeOndaz just as an experiment, you can try passing {phoneNumber: '+201142494391'} as the input. Then it should behave as expected.

hiranya911 avatar Aug 21 '21 00:08 hiranya911

If you normalize the phone number using E.164 spec before sending the request as @hiranya911 suggested, you shouldn't run into this issue. I think it may be too costly to add the dependency to normalize in the Admin SDK.

bojeil-google avatar Aug 21 '21 00:08 bojeil-google

Thank you @hiranya911 for your fast reply and effort. I've figured the same but with another approach, I deleted the user and recreated it, it actually worked but I noticed the hyphen thing at that time. I'll be normalizing this to a standard spec.

@bojeil-google Thank you. This is what I'm going to do for now.

LeOndaz avatar Aug 21 '21 00:08 LeOndaz

@hiranya91 @bojeil-google .. I noticed that getting the user directly with the phone numbers works directly, either with a hyphen or without. This doesn't apply to getUsers though. Just something to keep in mind

LeOndaz avatar Aug 21 '21 13:08 LeOndaz

@bojeil-google is there anything we should do in the SDK to resolve this? May be at least document that input phone numbers should be normalized?

hiranya911 avatar Sep 10 '21 20:09 hiranya911

Just to confirm, @LeOndaz are you saying getUserByPhoneNumber normalizes the phone number for you whereas getUsers doesn't?

bojeil-google avatar Sep 10 '21 21:09 bojeil-google

Just to confirm, @LeOndaz are you saying getUserByPhoneNumber normalizes the phone number for you whereas getUsers doesn't?

I think this is implied, yes

LeOndaz avatar Sep 10 '21 22:09 LeOndaz

This seems strange. They use the exact underlying API: https://cloud.google.com/identity-platform/docs/reference/rest/v1/projects.accounts/lookup In getUserByPhoneNumber we pass one phone number whereas in getUsers we pass multiple entries. Let me try it out myself on Monday.

@hiranya911 I don't think you want to add a dependency to normalize to E.164 (it is quite an expensive dependency). So our best option is to document this.

bojeil-google avatar Sep 11 '21 01:09 bojeil-google

@bojeil-google feel free to test, I think at least if you don't add the dependency, at least you should document this, are the docs available on any repo I have access to?

LeOndaz avatar Sep 11 '21 06:09 LeOndaz

Closing due to inactivity

lahirumaramba avatar Jan 17 '23 20:01 lahirumaramba