简体   繁体   中英

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());

                    }
                }
            });
}

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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM