Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobile OTP Verification without signing in using Firebase Phone Auth

I am currently making an android app where I need to verify if the user is entering correct mobile number using the OTP. The user is already signed in the application using his email and password. Now I need to verify the mobile number the user enters without using the signInWithCrendntial() method of firebase phone auth. How do i go about it ?

My mCallbacks is

                @Override
                public void onVerificationCompleted(PhoneAuthCredential credential) {
                    Toast.makeText(getApplicationContext(), "Verification Complete", Toast.LENGTH_SHORT).show();

                    showMessage("Success!!","OTP verified!" + credential);
                    cred = credential;
                    //btn_add_guest.setEnabled(true);

                }

                @Override
                public void onVerificationFailed(FirebaseException e) {
                    Toast.makeText(getApplicationContext(), "Verification Failed", Toast.LENGTH_SHORT).show();
                    Log.i(TAG,"Error is "+e.getMessage());
                }

                @Override
                public void onCodeSent(String verificationId,
                                       PhoneAuthProvider.ForceResendingToken token) {
                    Toast.makeText(getApplicationContext(), "Code Sent", Toast.LENGTH_SHORT).show();
                    mVerificationId = verificationId;
                    mResendToken = token;

                    Log.i(TAG,"VERFICATION ID IS"+mVerificationId);
                    Log.i(TAG,"RESEND TOKEN"+mResendToken);
                    btn_add_guest.setEnabled(false);
                }
            };

I m calling this method on button Pressed where put_otp is textView where user enters the OTP.

verifyPhoneNumberWithCode(mVerificationId,put_otp.getText().toString());
                    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, put_otp.getText().toString());
                    Log.i(TAG,credential.getProvider());

private void verifyPhoneNumberWithCode(String verificationId, String code) {

        Log.i(TAG,"RESEND TOKEN IN METHOD IS"+mResendToken);    if(code.equals(mResendToken)&&verificationId.equals(mVerificationId)){
            Toast.makeText(AddGuestActivity.this, "Verification Success", Toast.LENGTH_SHORT).show();

            btn_add_guest.setEnabled(true);
        }
        else
            Toast.makeText(this,"Please provide correct OTP",Toast.LENGTH_SHORT).show();
    }
like image 322
Khushboo Gandhi Avatar asked Oct 17 '22 05:10

Khushboo Gandhi


1 Answers

You can link an email/pass account with a phone account https://firebase.google.com/docs/auth/android/account-linking?authuser=0

like image 74
Markymark Avatar answered Oct 20 '22 05:10

Markymark