user9308549
user9308549

Reputation:

Cannot Verify phone number using firebase authentication

I m trying to verify phone number but it fails all time. I have Login Activity which takes all the information about user which includes mobile number field. After click on login button i want OTP activity will open and user can enter otp here and click on verify button to verify number. But it always go in onVerificationFailed block and shows toast Verification Fail and Invalid Number.

Login activity

public class Login extends AppCompatActivity{

public FirebaseAuth mAuth;
// [END declare_auth]

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


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


    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            final String name = txtName.getText().toString();
            final String mobileno = txtMobile.getText().toString();
            final String email = txtEmail.getText().toString();
            final String dob = txtDOB.getText().toString();
            final String address = txtAdd.getText().toString();
            final String city = txtCity.getText().toString();
            final String state = txtState.getText().toString();
            final String country = txtCountry.getText().toString();



                mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
                    @Override
                    public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                        Toast.makeText(Login.this,"Verification Done"+ phoneAuthCredential,Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onVerificationFailed(FirebaseException e) {
                        Toast.makeText(Login.this,"Verification Fail",Toast.LENGTH_LONG).show();
                        if (e instanceof FirebaseAuthInvalidCredentialsException){
                            Toast.makeText(Login.this, "Invalid Number", Toast.LENGTH_SHORT).show();
                        }else if (e instanceof FirebaseTooManyRequestsException){
                            Toast.makeText(Login.this, "Too many Request", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                        mVerificationId = s;
                        mResendToken = forceResendingToken;
                        Toast.makeText(Login.this, "Code Sent", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(Login.this, OTP.class));
                    }
                };
               PhoneAuthProvider.getInstance().verifyPhoneNumber(mobileno, 60, TimeUnit.SECONDS, Login.this, mCallbacks);
            }

        }
    });

}
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 informatio
                        Toast.makeText(Login.this,"Verification done",Toast.LENGTH_LONG).show();
                        FirebaseUser user = task.getResult().getUser();
                        // ...
                    } else {
                        // Sign in failed, display a message and update the UI
                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            // The verification code entered was invalid

                            Toast.makeText(Login.this,"Verification failed code invalid",Toast.LENGTH_LONG).show();
                        }
                    }
                }
            });
}
}

OTP activity

public class OTP extends AppCompatActivity {

public FirebaseAuth mAuth;
public String mVerificationId;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_otp);

    Button btnVerify = (Button) findViewById(R.id.btnVerifyOTP);
    final EditText txtOtp = (EditText) findViewById(R.id.txtOtp);
    btnVerify.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PhoneAuthCredential credential = PhoneAuthProvider.getCredential( mVerificationId  , txtOtp.getText().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(OTP.this,"Verification done",Toast.LENGTH_LONG).show();
                        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(OTP.this,"Verification failed code invalid",Toast.LENGTH_LONG).show();
                        }
                    }
                }
            });
}
}

Upvotes: 0

Views: 8042

Answers (2)

Rajasai Bandaru
Rajasai Bandaru

Reputation: 19

I had same problem ,what i did is while giving the input phone number say if number is 79.....6 , then give the input as +9179...6 (IF INDIAN NUMBER)

depending on the country , add country code as prefix to get sms code

Hope it helps

Upvotes: 1

Arnav Rao
Arnav Rao

Reputation: 6992

Firebase automatically verifies SMS message on some devices. In your case, it seems that it is trying to verify SMS message but couldn't do so, that is why invalid phone number error, I guess. But on which device are you testing?.

More importantly, your code doesn't seem to cover auto SMS verification scenario with this ... startActivity(new Intent(Login.this, OTP.class));

You can check complete implementation of Firebase phone number authentication here http://www.zoftino.com/firebase-phone-number-authentication-android

Upvotes: 0

Related Questions