Firebase phone Auth for Android , Can we just veri

2020-03-22 14:09发布

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

                    }
                }
            });
}

2条回答
祖国的老花朵
2楼-- · 2020-03-22 15:04

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.

查看更多
够拽才男人
3楼-- · 2020-03-22 15:06

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.

查看更多
登录 后发表回答