简体   繁体   中英

firebase phone auth android Crashing

so I am using firebase database and Auth, I am trying to build Phone Auth but for some reason the app is crashing.. not sure why.. I am new to programming with android studio and first timer on Firebase database, not sure why the app is crashing but its crashing after I am pressing a button that activate verifySignInCode() insta crash after it, it does send an email with code a Log cat Error is here

09-15 20:39:41.804 22046-22046/com.example.erelpc.calltest E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.erelpc.calltest, PID: 22046
    java.lang.IllegalArgumentException: Cannot create PhoneAuthCredential without either verificationProof, sessionInfo, ortemprary proof.
        at com.google.android.gms.common.internal.Preconditions.checkArgument(Unknown Source:8)
        at com.google.firebase.auth.PhoneAuthCredential.<init>(Unknown Source:6)
        at com.google.firebase.auth.PhoneAuthProvider.getCredential(Unknown Source:33)
        at com.example.erelpc.calltest.MainActivity.verifySignInCode(MainActivity.java:92)
        at com.example.erelpc.calltest.MainActivity.access$100(MainActivity.java:28)
        at com.example.erelpc.calltest.MainActivity$2.onClick(MainActivity.java:61)
        at android.view.View.performClick(View.java:6877)

And Here is the MainActivity

    /// SMS Handler
    private void sendVerificationCode(){
        String phone = etphonenumber.getText().toString();
        if (phone.isEmpty()) {
            etphonenumber.setError("Enter a Phone Number!");
            etphonenumber.requestFocus();
            return;
        }
        if (phone.length() != 9){
            etphonenumber.setError("Please Enter a valid Number!");
            etphonenumber.requestFocus();
            return;
        }
        phone = "+972" + phone;
        Intent intent =  new Intent(this, loginsuccess.class);
            PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phone,        // Phone number to verify
                60,                 // Timeout duration
                TimeUnit.SECONDS,   // Unit of timeout
                    this,               // Activity (for callback binding)
                    mCallbacks);        // OnVerificationStateChangedCallbacks
    }
    private void verifySignInCode(){

        String code = etcodeveri.getText().toString();
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codesent, code);
        signInWithPhoneAuthCredential(credential);
    }

    PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {

        }

        @Override
        public void onVerificationFailed(FirebaseException e) {

        }

        @Override
        public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
            super.onCodeSent(s, forceResendingToken);
            codesent = s;
        }
    };


    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(getApplicationContext(), "Login Success!", Toast.LENGTH_SHORT).show();
                            registerModule();
                        } else {
                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                                Toast.makeText(getApplicationContext(), "Login Unsuccess!", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });
    }  
    public void registerModule(){
        Intent intent =  new Intent(this, loginsuccess.class);
        startActivity(intent);
    }

The crashing is happening after I press the "btnAdd"

The app is crashing because the method is not getting valid credential for the phoneAuth to work. This can be fixed by using try and catch on the signInWithPhoneAuthCredential() method.

In code it will look something like this:

private void verifyCode(){

        String code = cd.getText().toString(); // this is OTP
        String pH = phone.getText().toString(); // this is phone number
        if(code.equals("") && pH.equals(""))
            Toast.makeText(MainActivity.this,"Nothing to validate", Toast.LENGTH_SHORT).show();
        else {
            try {
                PhoneAuthCredential credential = PhoneAuthProvider.getCredential(otp, code);
                signInWithPhoneAuthCredential(credential);
            }
            catch (Exception e) {
                Log.i("exception",e.toString());
                Toast.makeText(MainActivity.this,"Invalid credentials",Toast.LENGTH_LONG).show();
            }

        }
    }

Also, you should put more catch or if statements to avoid giving null value in the methods.

Just need to define String code before calling phoneAuth function.

< String code = phoneAuthCredential.getSmsCode();
            assert code != null;
            PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, code);

            signInWithPhoneAuthCredential(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