Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase phone Auth for Android , Can we just verify phone number without creating a user account

I am working on an android app, in where I just want to verify mobile number without creating a user account. Is it Possible? I am using the following code

   private void startPhoneNumberVerification(String phoneNumber) {

    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phoneNumber,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks);        // OnVerificationStateChangedCallbacks

}



private void verifyPhoneNumberWithCode(String verificationId, String code) {

    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);


     signInWithPhoneAuthCredential(credential); // this function is creating user account , if not present. But We Don't want this


}

The following function will create user account if user account is not there, but I don't want to create account, I just want to verify the code entered by the user. Is there any call back method available for that?

private void signInWithPhoneAuthCredential(final PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {



                        dialog.dismiss();
                        FirebaseUser user = task.getResult().getUser();

                        Toast.makeText(LoginActivity.this, "Success " + user.getEmail(), Toast.LENGTH_SHORT).show();


                    } else {

                        Toast.makeText(LoginActivity.this, "Failed ", Toast.LENGTH_SHORT).show();

                        verifyPhoneNumberWithCode(mVerificationId, editText.getText().toString().trim());

                    }
                }
            });
}
like image 579
alka aswal Avatar asked Jun 12 '17 10:06

alka aswal


People also ask

How do I verify my number on Firebase?

In the Firebase console, open the Authentication section. In the Sign in method tab, enable the Phone provider if you haven't already. Open the Phone numbers for testing accordion menu. Provide the phone number you want to test, for example: +1 650-555-3434.

How do I authenticate a phone number?

Phone-based authentication involves sending a one-time password (OTP) to a user over a separate communication channel (e.g. SMS, MMS, WhatsApp, Facebook Messenger, Viber or even voice) from the IP channel (internet) used by the application, providing security in case the IP channel is compromised.

How do I authenticate my Android phone?

In phone authentication, the user has to verify his identity with his phone number. Inside the app user has to enter his phone number after that he will receive a verification code on his mobile number. He has to enter that verification code and verify his identity.

Can I use Firebase for authentication only?

Yes, you can use Firebase for auth only and link it to your own database. You'll have to use the Firebase Admin SDK in your backend. Check out the set up guide.


3 Answers

You can't verify what the user typed without linking the phone provider to the Firebase user in the process.

But you can unlink the phone from the user account soon after, by calling:

FirebaseAuth.getInstance().getCurrentUser().
        unlink(PhoneAuthProvider.PROVIDER_ID)
        .addOnCompleteListener(this, onCompleteListener);

There are plenty of uses for verifying that the user has access to this phone number, but shouldn't login with it. I really think that Firebase should allow developers to verify first, and use the credential to login after.

Also:

There is a good chance of Google Play Services verifying automatically. When onVerificationCompleted(PhoneAuthCredential) in your PhoneAuthProvider.OnVerificationStateChangedCallbacks is called. This way the user won't need to type the verification code, and the phone won't be linked automatically.

like image 103
Vitor Hugo Schwaab Avatar answered Nov 15 '22 18:11

Vitor Hugo Schwaab


Verifying a phone number automatically creates a Firebase Authentication account for that user. There is no way to prevent creating this account, as it is what Firebase uses to ensure it knows that user next time they start the app.

like image 30
Frank van Puffelen Avatar answered Nov 15 '22 19:11

Frank van Puffelen


You can also update the phone number on the user account.

This will not create new user; instead, it will update the phone number in the existing user account.

val credential = PhoneAuthProvider.getCredential(verificationId!!, smsCode)
FirebaseAuth.getInstance().currentUser?.updatePhoneNumber(credential)
like image 40
Amit Das Avatar answered Nov 15 '22 19:11

Amit Das