0

I created an app in the android studio and linked it with Firebase Realtime database. I have used its Notifications service to send OTP to authenticate the user mobile no.

The problem I am facing is that after logout, I want to send OTP again to the number entered by the user but instead of that it is verifying the number and automatically opening the activity. Can anyone help me out ???

Steve Vinoski
  • 19,847
  • 3
  • 31
  • 46

1 Answers1

2

This method can send OTP to your Mo. number

    PhoneAuthProvider.getInstance().verifyPhoneNumber(
    phoneNumber,        // Phone number to verify
    60,                 // Timeout duration
    TimeUnit.SECONDS,   // Unit of timeout
    this,               // Activity (for callback binding)
    mCallbacks);  

You require call back to getting response

    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 verification without
    //     user action.
    Log.d(TAG, "onVerificationCompleted:" + credential);

    signInWithPhoneAuthCredential(credential);
}

@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);

    if (e instanceof FirebaseAuthInvalidCredentialsException) {
        // Invalid request
        // ...
    } else if (e instanceof FirebaseTooManyRequestsException) {
        // The SMS quota for the project has been exceeded
        // ...
    }

    // 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);

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

    // ...
}};

And for verification of OTP call below method

 PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);


 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");

                    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
                    }
                }
            }
        });}
Chetan Shelake
  • 656
  • 1
  • 6
  • 14
  • I have created an android app and i want to verify otp only once(suppose if a same person have 2 cell phone and wants to play the app with second phone ,then number should be verified automatically from the firebase and otp should not be sent to the same number)help me out with this please. – Mohit Bhardwaj Mar 15 '18 at 06:38
  • I think it is not possible if you want to login again then you should sign in with phone number or email-password then every time firebase send OTP and verify it again – Chetan Shelake Mar 15 '18 at 07:36
  • i want to validate the phone number is there in firebase or not ,if not access should not be granted to next activityand if number is there then access should be automatic – Mohit Bhardwaj Mar 15 '18 at 08:45
  • need to add home button to myapp bar and calling activityt from this – Mohit Bhardwaj Mar 15 '18 at 08:52