Search code examples
firebase-authenticationfirebaseui

Is a full sign-in required to verify a phone number using Firebase Phone Auth?


A question about firebase auth for authentication via phone number.

I was wondering if it was possible to link a "Phone Provider" to say a Google Auth Provider. There is no explicit mention of it in the docs.

The thing that had me scratching my head is - the Link Multiple Auth Provider docs talk about starting to authenticate with the new provider (phone provider) you want to link to the existing provider (google provider), but then stopping short of calling FirebaseAuth.signInWithXXX.

So in theory that would work like:

  1. The user logs in via google (google idp provider)
  2. The user kicks off the phone auth (phone number provider) - and gets an sms message.
  3. The SMS should trigger an automatic verification in some cases or he types in the 6 digit code from the sms message
  4. But based on the docs on account-linking, rather than call FirebaseAuth.signInWithXXX here, we can instead call FirebaseUser.linkWithCredential(PhoneAuthCredential).

So i was wondering if a phone number verification is considered complete without an explicit sign-in with the PhoneAuthCredential ?


Solution

  • You can link a PhoneAuthCredential to an existing user, in your case, a user with a GoogleAuthProvider.

    After you sign in the user with Google. You then go through PhoneAuthProvider.getInstance().verifyPhoneNumber(phoneNumber, ...) This would resolve with a PhoneAuthCredential or a verification ID. You can then ask for the SMS code and instantiate a PhoneAuthCredential via PhoneAuthProvider.getCredential.

    You can then link that credential to the currentUser: currentUser.linkWithCredential(phoneAuthCredential)