0

我通过电话号码创建了一个连接,一切都很好。我唯一的问题是这个数字没有出现在 firebase 的 ID 中。

使用谷歌身份验证它工作得很好,但没有电话身份验证。我可能忘记了什么,但我找不到。

你有想法吗?

在 firebase 和 gmail ID 上: 在此处输入图像描述

我的活动授权电话:

public class Activity_aunth_phone extends AppCompatActivity {

        EditText numero;
        String verificationId;
        FirebaseAuth mAuth;
        Button button;
        EditText cp;


        private static final String TAG = "PhoneAuthActivity";

        private static final String KEY_VERIFY_IN_PROGRESS = "key_verify_in_progress";
        private static final int STATE_INTIALIZED = 1;
        private static final int STATE_CODE_SEND = 2;
        private static final int STATE_VERIFY_FAILED = 3;
        private static final int STATE_VERIFY_SUCCES = 4;
        private static final int STATE_SIGNIN_FAILED = 5;
        private static final int STATE_SIGNIN_SUCCES = 6;
        private FirebaseAuth auth;
        private boolean mVerificationInProgress = false;
        private String mVerificationId;
        private PhoneAuthProvider.ForceResendingToken mResendToken;

        private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_aunth_phone);

            cp = findViewById(R.id.cp);
            numero = (EditText) findViewById(R.id.num);
            button = findViewById(R.id.button);

            auth = FirebaseAuth.getInstance();



            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                attemptLogin();
                }
            });


            mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

                @Override
                public void onVerificationCompleted(PhoneAuthCredential credential) {
                    // This callback will be invoked in two situations:
                    // 1 - Instant verification. In some cases the phone number can be instantly
                    //     verified without needing to send or enter a verification code.
                    // 2 - Auto-retrieval. On some devices Google Play services can automatically
                    //     detect the incoming verification SMS and perform verification without
                    //     user action.
                    Log.d(TAG, "onVerificationCompleted:" + credential);

                    signInWithPhoneAuthCredential(credential);
                }

                @Override
                public void onVerificationFailed(FirebaseException e) {
                    // This callback is invoked in an invalid request for verification is made,
                    // for instance if the the phone number format is not valid.
                    Log.w(TAG, "onVerificationFailed", e);

                    if (e instanceof FirebaseAuthInvalidCredentialsException) {
                        // Invalid request
                        // ...
                    } else if (e instanceof FirebaseTooManyRequestsException) {
                        // The SMS quota for the project has been exceeded
                        // ...
                    }

                    // Show a message and update the UI
                    // ...
                }

                @Override
                public void onCodeSent(String verificationId,
                                       PhoneAuthProvider.ForceResendingToken token) {
                    // The SMS verification code has been sent to the provided phone number, we
                    // now need to ask the user to enter the code and then construct a credential
                    // by combining the code with a verification ID.
                    Log.d(TAG, "onCodeSent:" + verificationId);

                    // Save verification ID and resending token so we can use them later
                    mVerificationId = verificationId;
                    mResendToken = token;

                    // ...
                }
        };

        }
        private void attemptLogin() {
            numero.setError(null);

            String cpp = cp.getText().toString();
            String number = numero.getText().toString();

            String phone = cpp + number;

            startPhoneNumberVerification(phone);


            boolean cancel = false;
            View focusView = null;

            if (!isPhoneValid(phone)) {
                focusView = numero;
                cancel = true;
            }
            if (cancel) {
    focusView.requestFocus();
            } else {
                startPhoneNumberVerification(phone);
            }
        }

        private boolean isPhoneValid(String phone){
            return phone.length() > 8;
        }
        private void startPhoneNumberVerification(String phoneNumber) {
            PhoneAuthProvider.getInstance().verifyPhoneNumber(
                    phoneNumber,
                    60,
                    TimeUnit.SECONDS,
                    this,
                    mCallbacks);

            mVerificationInProgress = true;
        }
        private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
            mAuth.signInWithCredential(credential)
                    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                // Sign in success, update UI with the signed-in user's information
                                Log.d(TAG, "signInWithCredential:success");
    Intent intent = new Intent(Activity_aunth_phone.this, Activity_profile.class);
    startActivity(intent);
                                FirebaseUser user = task.getResult().getUser();
                                // ...
                            } else {
                                // Sign in failed, display a message and update the UI
                                Log.w(TAG, "signInWithCredential:failure", task.getException());
                                if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                                    // The verification code entered was invalid
                                }
                            }
                        }
                    });
        }

    }
4

1 回答 1

2

你忘了把代码:

private void verifyPhoneNumberWithCode(String verificationId, String code) {
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, code); // Just Pass the otp in Code Section.
        signInWithPhoneAuthCredential(credential);
    }

它为新用户提供凭据。

请检查此链接:https ://firebase.google.com/docs/auth/android/phone-auth?authuser=1#create-a-phoneauthcredential-object

于 2019-08-05T05:22:36.007 回答