getUsers(identifiers) returns the same user in .notFound and in .users
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.
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.
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 added
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.
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.
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.
@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
@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?
Just to confirm, @LeOndaz are you saying getUserByPhoneNumber normalizes the phone number for you whereas getUsers doesn't?
Just to confirm, @LeOndaz are you saying
getUserByPhoneNumbernormalizes the phone number for you whereasgetUsersdoesn't?
I think this is implied, yes
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 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?
Closing due to inactivity