Ad

How To Know If Phone Number Already Linked With The Password Account In Firebase Web Before OTP Sent To User

- 1 answer

I am creating an ionic application with firebase authentication. I provided 'facebook login' and 'password authentication' feature. When the user tries to login via the above two features, I want them to link their phone number with their Facebook or password authentication account. I had implemented the feature of linking phone number with their account.

My requirement here is: I want to check if the phone number they are trying to link is already linked by some other user in the firebase User or not. If not I allow them to link else I won't. Also, I want this result before OTP sent to the user.

this.confirmationResult = this.auth.userCredential.user. linkWithPhoneNumber(this.phoneNumber, this.appVerifier);

I expect the above code should return an error if the phone number is already linked with some other account. But, It allows sending the OTP to the user's phone number and then prompts me to provide the code sent to the phone number. Also, I can able to receive the error code after verify the OTP like "auth/credential-already-in-use". I want this before code sent to the user's phone number. Please help me to achieve this scenario.

Ad

Answer

You can use the Firebase Admin SDK to lookup whether a user with a phone number already exists.

Here is an example with Node.js

admin.auth().getUserByPhoneNumber(phoneNumber)
  .then(function(userRecord) {
    // A user with the phone number already exists.
    // You can also get other information related to the user from the
    // userRecord.
  })
  .catch(function(error) {
    console.log('Error fetching user data:', error);
  });
Ad
source: stackoverflow.com
Ad