Firebase ссылка электронной почты на телефон: невозможно создать PhoneAuthCredential без проверки Proof - PullRequest
1 голос
/ 01 мая 2019

Итак, мой Phone Verify работает с тех пор, как я обновил свой Firebase на прошлой неделе.С тех пор я сталкиваюсь с проблемой, заключающейся в том, что в процессе связывания, когда я подключаю электронную почту пользователей к номеру телефона, больше не работает: java.lang.IllegalArgumentException: невозможно создать PhoneAuthCredential без проверкиProof, sessionInfo или временного подтверждения.Я видел некоторых пользователей с такой же проблемой, но ни у кого нет решения.

Я пытался переписать весь Код, но проблема все еще существует.Firebase изменил что-то в процессе компоновки?Как я видел в документации по связыванию Firebase, раздел о связывании телефонных номеров был удален.

Что-то не так с моим кодом или проблема с firebase?

Firebase Versions I'm using:

implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-messaging:17.6.0'
implementation 'com.google.firebase:firebase-perf:16.2.5'
implementation 'com.android.support:support-compat:28.0.0'
implementation 'com.google.firebase:firebase-auth:16.2.1'
implementation 'com.google.firebase:firebase-storage:16.1.0'
   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.user_phone_verify);
   Log.e("PhoneVerify","Start");

        mAuth = FirebaseAuth.getInstance();

        editTextCode = findViewById(R.id.editTextCode);
        editTextPhone = findViewById(R.id.editTextPhone);

        Bundle extras = getIntent().getExtras();
        if (extras !=null) {
            final String phone = extras.getString("phone");
            Log.e("Phone(Extras):",phone);
            editTextPhone.setText(phone);
            sendVerificationCode(phone);
        }


        findViewById(R.id.buttonGetVerificationCode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = editTextPhone.getText().toString().trim();
                if (phone.isEmpty() || phone.length() < 10) {
                    editTextPhone.setError("Phone number error");
                    editTextPhone.requestFocus();
                    return;
                }

                 sendVerificationCode(phone);
            }
        });


        findViewById(R.id.buttonSignIn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verifyVerificationCode(editTextCode.getText().toString());
            }
        });
    }


    private void sendVerificationCode(String phonenumber) {
        String phone = "+14" + phonenumber;
        Log.e("sendVerificationCode",phone);
        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phone,        // Phone number to verify
                60,                 // Timeout duration
                TimeUnit.SECONDS,   // Unit of timeout
                this,               // Activity (for callback binding)
                mCallbacks);        // OnVerificationStateChangedCallbacks
    }



    PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        @Override
        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {

             //Getting the code sent by SMS
            final String code = phoneAuthCredential.getSmsCode();

            if (code != null) {
                editTextCode.setText(code);
                //verifying the code
                verifyVerificationCode(code);
                Log.e("onVerificationCompleted",code);
            }

        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            Log.e("onVerificationFailed", String.valueOf(e));

        }

        @Override
        public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
            super.onCodeSent(s, forceResendingToken);
            Log.e("onCodeSent", "Code Sent");
            codeSent = s;
            PhoneAuthProvider.ForceResendingToken mResendToken = forceResendingToken;
        }
    };



    private void verifyVerificationCode(String code) {
        //creating the credential
        try {

            PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, code);
            linkWithCredential(credential,code);

        } catch (Exception e) {
            Log.e("Exception", String.valueOf(e));
        }

        Log.e("VerifyCode CHECKP",code);
        //signing the user

    }

    private void linkWithCredential(final AuthCredential credential, final String code) {
        mAuth.getCurrentUser().linkWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                Log.e("Linking Phone to Email","Successfull");

                try {

                    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, code);
                    signInWithPhoneAuthCredential(credential);

                } catch (Exception e) {
                    Log.e("Exception", String.valueOf(e));
                }

            }
        });
    }


    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(PhoneVerify.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            //verification successful we will start the profile activity

                            Log.e("FINAL LINK","DONE");


                        } else {
                            //verification unsuccessful.. display an error message

                            String message = "Somthing is wrong, we will fix it soon...";



                });
    }



    }

Ответы [ 2 ]

0 голосов
/ 01 мая 2019

Здесь показано, как использовать проверку подлинности телефона Firebase из документов Firebase
https://firebase.google.com/docs/auth/android/phone-auth
и связывание провайдера аутентификации
https://firebase.google.com/docs/auth/android/account-linking

//please notice, mCallback.onVerificationCompleted will automatically called 
//if verification code already send to your phone 
//(Maybe not called in some case)
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

    @Override
    public void onVerificationCompleted(PhoneAuthCredential credential) {
        linkPhoneAuthToCurrentUser(credential);
    }

    @Override
    public void onVerificationFailed(FirebaseException e) {
        //show error
    }
    @Override
    public void onCodeSent(String verificationId,
                           PhoneAuthProvider.ForceResendingToken token) {
        //save id and token in case you need to resend verification code
    }
};

//call this to send verification code
//parameter include country code
private void sendVerificationCode(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
}

//link auth with credential
private void linkPhoneAuthToCurrentUser(PhoneAuthCredential credential) {
    FirebaseAuth.getInstance().getCurrentUser().linkWithCredential(credential)
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    //link auth success update ui
                } else {
                    //link auth failed update ui
                }
            }
        });
}
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.user_phone_verify);

   //init view 

   //get phone number
   String phoneNumb = "Add Your Phone Number Here"
   sendVerificationCode(phoneNumb)

   //if mCallback not called, 
   //use this button to sign in with manual input verification code
   btnSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //get verification id from mCallback.onCodeSent
            //get verificationCode manually from edittext 
            PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, verificationCode);
            linkPhoneAuthToCurrentUser(credential);
        }
    });
}

PS: перед аутентификацией по номеру телефона необходимо выполнить повторную проверку подлинности электронной почты.
Перейдите по этой ссылке для повторной аутентификации
https://firebase.google.com/docs/auth/android/manage-users#re-authenticate_a_user

0 голосов
/ 01 мая 2019

onVerificationCompleted вызывается, когда firebase автоматически проверяет номер телефона, обнаруживая код SMS, отправленный на телефон.Я думаю, вам следует переформатировать свой код, чтобы использовать здесь только учетные данные или тот, который вы генерируете вручную, путем комбинации кода, введенного пользователем, и validId;и не оба.Также не нужно входить снова с учетными данными телефона, вы уже вошли в систему

Я имею в виду, что вы должны иметь что-то вроде этого,

private void linkWithCredential(final PhoneAuthCredential credential) {
        mAuth.getCurrentUser().linkWithCredential(credential).addOnComplete.......{
            onComplete(....) {
                .....
                //signInWithPhoneCredential(credential);
                // You Already signed in. No need. Just update the ui with the user info
    }

https://firebase.google.com/docs/auth/android/account-linking

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...