Rakesh
Rakesh

Reputation: 1035

Does SMS Retriever API is required in Firebase authentication for otp auto fill?

I am using Firebase auth api to verify user phone number. Currently in my case I have one screen where user put phone number and on second screen otp. As per documentation firebase automatic retrieve otp and start verification process most of time. So my question is it already implemented this SMS Retriever API in Firebase Auth SDK or Should I implement it myself to retrieve SMS and auto fill OTP.

Upvotes: 8

Views: 4056

Answers (1)

Rumit Patel
Rumit Patel

Reputation: 12539

Nope. we don't need to manage SMS-retrival scenario.

If device contains same SIM-card, It's automatically managed by PhoneAuthProvider.OnVerificationStateChangedCallbacks in onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) method.

Snippet:

private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        @Override
        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
            Toast.makeText(FCMsmsTest.this, "onVerificationCompleted " + phoneAuthCredential.toString(), Toast.LENGTH_SHORT).show();
            signInWithPhoneAuthCredential(phoneAuthCredential);
        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            Toast.makeText(FCMsmsTest.this, "onVerificationFailed " + e.toString(), Toast.LENGTH_SHORT).show();

            if (e instanceof FirebaseAuthInvalidCredentialsException) {
                Toast.makeText(FCMsmsTest.this, "Invalid Request " + e.toString(), Toast.LENGTH_SHORT).show();
            } else if (e instanceof FirebaseTooManyRequestsException) {
                Toast.makeText(FCMsmsTest.this, "The SMS quota for the project has been exceeded " + e.toString(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCodeSent(String verificationId,
                               PhoneAuthProvider.ForceResendingToken token) {
            Toast.makeText(FCMsmsTest.this, "onCodeSent " + verificationId, Toast.LENGTH_SHORT).show();
            editText.setText("");

            mVerificationId = verificationId;
            PhoneAuthProvider.ForceResendingToken mResendToken = token;

            showDialog();
        }
    };

Upvotes: 4

Related Questions