0
public class MainActivity extends AppCompatActivity  {

EditText editTextPhone, editTextCode;

FirebaseAuth mAuth;

String codeSent;

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

    mAuth = FirebaseAuth.getInstance();

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

    findViewById(R.id.buttonGetVerificationCode).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendVerificationCode();
        }
    });


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

private void verifySignInCode(){
    String code = editTextCode.getText().toString();
    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, code);
    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()) {
                       //here you can open new activity
                        Toast.makeText(getApplicationContext(),
                                "Login Successfull", Toast.LENGTH_LONG).show();
                    } else {
                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            Toast.makeText(getApplicationContext(),
                                    "Incorrect Verification Code ", Toast.LENGTH_LONG).show();
                        }
                    }
                }
            });
}

private void sendVerificationCode(){

    String phone = editTextPhone.getText().toString();

    if(phone.isEmpty()){
        editTextPhone.setError("Phone number is required");
        editTextPhone.requestFocus();
        return;
    }

    if(phone.length() < 10 ){
        editTextPhone.setError("Please enter a valid phone");
        editTextPhone.requestFocus();
        return;
    }


    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) {

    }

    @Override
    public void onVerificationFailed(FirebaseException e) {

    }

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

        codeSent = s;
    }
};

}

Firebase not sending a auth code to mobile for verification please check what is the problem

KENdi
  • 7,576
  • 2
  • 16
  • 31
Rover
  • 661
  • 2
  • 18
  • 39

1 Answers1

1

because if your already regiter a number then 2nd time doesnot send if you want to try them to get otp then delete a number from firebase auth and try again

axar
  • 539
  • 2
  • 17