0

I'm trying to make and app login page using firebase phone authentication on android. I have successfully sent a phone number to firebase and also received an OTP code. But whenever I input the otp code and hit the verify button I get the error. Please help.

package com.saywhat.tabdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.FirebaseTooManyRequestsException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;

import java.util.concurrent.TimeUnit;

public class Login extends AppCompatActivity {

    EditText MobileNumber, OTPEditTextview;
    Button Submit, OTPButton;
    String countryCode = "+63";
    TextView countryCodeTV, enterPhoneTV, Otp;

    private boolean mVerificationInProgress = false;
    private FirebaseAuth mAuth;
    private static final String TAG = "PhoneLogin";

    private String mVerificationId;
    private PhoneAuthProvider.ForceResendingToken mResendToken;
    private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;

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

        MobileNumber = (EditText) findViewById(R.id.mobileNumber);
        Submit = (Button) findViewById(R.id.submit);
        OTPEditTextview = (EditText) findViewById(R.id.otp_editText);
        OTPButton = (Button) findViewById(R.id.otp_button);
        countryCodeTV = (TextView) findViewById(R.id.countryCode);
        enterPhoneTV = (TextView) findViewById(R.id.enterPhone);
        Otp = (TextView) findViewById(R.id.otp);

        //mCallbacks - start
        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 verificaiton without
                //     user action.
                Log.d(TAG, "onVerificationCompleted:" + credential);
                Toast.makeText(Login.this, "Verification Completed", Toast.LENGTH_SHORT).show();


            }

            @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);
                Toast.makeText(Login.this, "Verification Failed", Toast.LENGTH_SHORT).show();

                if (e instanceof FirebaseAuthInvalidCredentialsException) {
                    // Invalid request
                    Toast.makeText(Login.this, "Invalid Phone Number", Toast.LENGTH_SHORT).show();
                    // ...
                } else if (e instanceof FirebaseTooManyRequestsException) {
                    // The SMS quota for the project has been exceeded
                    Toast.makeText(Login.this, "SMS Quota Exceeded", Toast.LENGTH_SHORT).show();
                    // ...
                }

                // 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);
                Toast.makeText(Login.this, "Verification code sent", Toast.LENGTH_SHORT).show();

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

                // Send Phone Number views - start
                MobileNumber.setVisibility(View.GONE);
                Submit.setVisibility(View.GONE);
                countryCodeTV.setVisibility(View.GONE);
                enterPhoneTV.setVisibility(View.GONE);
                // Send Phone Number views - end

                // Verify OTP views - start
                Otp.setVisibility(View.VISIBLE);
                OTPButton.setVisibility(View.VISIBLE);
                OTPEditTextview.setVisibility(View.VISIBLE);
                // Verify OTP views - end

                // ...
            }
        };
        //mCallbacks - end


        Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, countryCode + MobileNumber.getText().toString());
                PhoneAuthProvider.getInstance().verifyPhoneNumber(
                        countryCode + MobileNumber.getText().toString(),
                        60,
                        TimeUnit.SECONDS,
                        Login.this,
                        mCallbacks);
            }
        });

        OTPButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code); - ORIGINAL
                PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, OTPEditTextview.getText().toString());
                Log.d(TAG, OTPEditTextview.getText().toString());
                Log.d(TAG, credential.toString());
                signInWithPhoneAuthCredential(credential);
            }
        });
    }





    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");
                            Toast.makeText(Login.this, "Signing in", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(Login.this, com.saywhat.tabdemo.MainActivity.class));

                            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
                                Toast.makeText(Login.this, "Verification code invalid", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });
    }
}

My Logcat shows

22:23:06.441 8184-8184/com.saywhat.tabdemo E/AndroidRuntime: FATAL EXCEPTION: main Process: com.saywhat.tabdemo, PID: 8184 java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.firebase.auth.FirebaseAuth.signInWithCredential(com.google.firebase.auth.AuthCredential)' on a null object reference at com.saywhat.tabdemo.Login.signInWithPhoneAuthCredential(Login.java:155) at com.saywhat.tabdemo.Login.access$300(Login.java:27) at com.saywhat.tabdemo.Login$3.onClick(Login.java:145) at android.view.View.performClick(View.java:5702) at android.widget.TextView.performClick(TextView.java:10888) at android.view.View$PerformClick.run(View.java:22541) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7229) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

0 Answers0