Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Phone Authentication not working after creating a signed APK

My app worked fined until I generated a signed APK in Android Studio when I do Authentication on signed Apk it just shows me a Toast "cannot verify phone and create account". It worked fine in debug mode.and when I made a debug APK it also ran fine in it, but not in Signed APK. I am unable to figure out what is causing this error that Authentication is not working in just Signed APK

My code

   public class Register2 extends AppCompatActivity {

    FirebaseAuth auth;
    DatabaseReference reference;

    String otpCode;
    String verificationId;
    MaterialEditText phone, optEnter;
    Button next;
    CountryCodePicker countryCodePicker;

    PhoneAuthCredential credential;
    Boolean verificationOnProgress = false;
    ProgressBar progressBar;
    TextView resend;

    PhoneAuthProvider.ForceResendingToken token;

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

        phone = findViewById(R.id.phone);
        optEnter = findViewById(R.id.codeEnter);
        countryCodePicker = findViewById(R.id.ccp);
        next = findViewById(R.id.nextBtn);
        auth = FirebaseAuth.getInstance();
        progressBar = findViewById(R.id.progressBar);
        resend = findViewById(R.id.resendOtpBtn);

        resend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // todo:: resend OTP
            }
        });

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

                if (!phone.getText().toString().isEmpty() && phone.getText().toString().length() == 10) {
                    if (!verificationOnProgress) {
                        next.setEnabled(false);
                        String phoneNum = "+" + countryCodePicker.getSelectedCountryCode() + phone.getText().toString();
                        Log.d("phone", "Phone No.: " + phoneNum);
                        progressBar.setVisibility(View.VISIBLE);
                        requestPhoneAuth(phoneNum);
                    } else {
                        next.setEnabled(false);
                        optEnter.setVisibility(View.GONE);
                        progressBar.setVisibility(View.VISIBLE);
                        otpCode = optEnter.getText().toString();
                        if (otpCode.isEmpty()) {
                            optEnter.setError("Required");
                            return;
                        }

                        credential = PhoneAuthProvider.getCredential(verificationId, otpCode);
                        verifyAuth(credential);
                    }

                } else {
                    phone.setError("Valid Phone Required");
                }
            }
        });
    }

    private void requestPhoneAuth(String phoneNumber) {
        PhoneAuthProvider.getInstance().verifyPhoneNumber(phoneNumber, 60L, TimeUnit.SECONDS, this,
                new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

                    @Override
                    public void onCodeAutoRetrievalTimeOut(String s) {
                        super.onCodeAutoRetrievalTimeOut(s);
                        Toast.makeText(Register2.this, "OTP Timeout, Please Re-generate the OTP Again.", Toast.LENGTH_SHORT).show();
                        resend.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                        super.onCodeSent(s, forceResendingToken);
                        verificationId = s;
                        token = forceResendingToken;
                        verificationOnProgress = true;
                        progressBar.setVisibility(View.GONE);
                        next.setText("Verify");
                        next.setEnabled(true);
                        optEnter.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {


                        verifyAuth(phoneAuthCredential);


                    }

                    @Override
                    public void onVerificationFailed(FirebaseException e) {
                        Toast.makeText(Register2.this, e.getMessage(), Toast.LENGTH_SHORT).show();

                    }
                });
    }


    private void verifyAuth(PhoneAuthCredential credential) {
        auth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {

                    Toast.makeText(Register2.this, "Phone Verified", Toast.LENGTH_SHORT).show();

                    FirebaseUser firebaseUser = auth.getCurrentUser();
                    String userid = firebaseUser.getUid();
                    reference = FirebaseDatabase.getInstance().getReference().child("UserIds").child(userid);
                    reference.setValue(true);


                    //checkUserProfile();


                } else {
                    progressBar.setVisibility(View.GONE);
                    Toast.makeText(Register2.this, "Can not Verify phone and Create Account.", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();

        if (auth.getCurrentUser() != null) {
            progressBar.setVisibility(View.VISIBLE);

            checkUserProfile();
        }
    }


    private void checkUserProfile() {


        Intent intent = new Intent(Register2.this, Details3.class);
        startActivity(intent);

    }

}
like image 827
Shivam Singh Avatar asked Oct 15 '22 03:10

Shivam Singh


1 Answers

After uploading the application to google play store, you have to update the SHA-1/SHA-255 fingerprint in firebase. You'll find the SHA-1/SHA-255 in play store as given.

  1. Navigate to the Google Play Console and login
  2. Choose the application you are signing
  3. Go to Release Management > App Signing
  4. Copy /Download the SHA-1 or SHA-256 certificate fingerprint from the App signing certificate section

Screenshot

Now, add the SHA-1/SHA-255 to the firebase and your app will be back again.

like image 191
Kiran Maniya Avatar answered Oct 27 '22 01:10

Kiran Maniya