Firebase not sending OTP

混江龙づ霸主 提交于 2020-04-10 05:07:06

问题


I had registered a user in firebase using Firebase phone number authentication.For testing that functionality again, I deleted the user account from Firebase console. Now, i am trying to register that number again, its directly going to the onVerificationCompleted() callback without sending the OTP.But the user account is not shown in Firebase console.

Please help.

Code given below. Also please note that its sending OTP to the new numbers .But not sending OTP to the number which i deleted from firebase console.I want to register that particular number again.

public class MainActivity extends AppCompatActivity {

private FirebaseAuth mAuth;

private boolean mVerificationInProgress = false;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;

private static final String KEY_VERIFY_IN_PROGRESS = "key_verify_in_progress";

private static final String TAG = "PhoneAuthActivity";
private String mVerificationId;
private PhoneAuthProvider.ForceResendingToken mResendToken;

private EditText mPhoneNumberField;

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

    if (savedInstanceState != null) {
        onRestoreInstanceState(savedInstanceState);
    }

    mPhoneNumberField=(EditText)findViewById(R.id.phone_number);

    mAuth = FirebaseAuth.getInstance();

    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);
            Toast.makeText(getApplication(),"Verification completed",Toast.LENGTH_SHORT).show();
            // [START_EXCLUDE silent]
            mVerificationInProgress = false;

            Log.d(TAG,"CREDENTIAL"+credential);

//                Intent intent=new 
Intent(getBaseContext(),VerificationActivity.class);
//                intent.putExtra("VERIFICATION_ID",mVerificationId);
//                startActivity(intent);


        }




        @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);
            // [START_EXCLUDE silent]
            mVerificationInProgress = false;
            // [END_EXCLUDE]

            if (e instanceof FirebaseAuthInvalidCredentialsException) {
                // Invalid request
                // [START_EXCLUDE]
                mPhoneNumberField.setError("Invalid phone number.");
                // [END_EXCLUDE]
            } else if (e instanceof FirebaseTooManyRequestsException) {
                // The SMS quota for the project has been exceeded
                // [START_EXCLUDE]
                Snackbar.make(findViewById(android.R.id.content), "Quota exceeded.",
                        Snackbar.LENGTH_SHORT).show();
                // [END_EXCLUDE]
            }


        }

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

            Intent intent=new Intent(getBaseContext(),VerificationActivity.class);
            intent.putExtra("VERIFICATION_ID",mVerificationId);
            startActivity(intent);


        }
    };
}


public void verifyDevice(View v){

    String phno=mPhoneNumberField.getText().toString();
    startPhoneNumberVerification(phno);


}

private void startPhoneNumberVerification(String phoneNumber) {
    // [START start_phone_auth]
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phoneNumber,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks);        // OnVerificationStateChangedCallbacks
    // [END start_phone_auth]

    mVerificationInProgress = true;


}


@Override
public void onStart() {
    super.onStart();

    if (mVerificationInProgress) {
        startPhoneNumberVerification(mPhoneNumberField.getText().toString());
    }
    // [END_EXCLUDE]
}
// [END on_start_check_user]

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean(KEY_VERIFY_IN_PROGRESS, mVerificationInProgress);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    mVerificationInProgress = savedInstanceState.getBoolean(KEY_VERIFY_IN_PROGRESS);
}

Looking forward to some help.


回答1:


Try to signout of your app or simply clear data or reinstall, it's possible there is a little glitch somewhere within the app.

If the above instruction doesn't work, then the cause is a little deeper. Since the authentication worked the first time, it's most likely a network carrier issue; this happens mostly in cases where a phone number was ported from one network provider to another at some point. You can try experimenting with phone numbers of a different network carrier. Sadly, there is no exact workaround for this problem yet. Whichever case, you can send your complaints to Firebase support. There'd definitely be some help there.




回答2:


As per my experience, Firebase creates a refresh token & a firebase user ID token after login, firebase user ID token expires every hour whereas refresh token is long-lived.

this firebase user ID token resides in user device & refresh token is maintained on firebase servers.

In your case after deleting user account refresh token must have expired by firebase but user ID token on your device must not have expired and that's why it automatically calls onVerificationCompleted()

You can read about managing sessions & tokens here




回答3:


Worked for me .

Sometimes Google is unable to verify the request coming from you app. In order to solve it. Generate your app's signing report and get your SHA-1 or SHA-256 fingerprints. Go to your firebase project setting and update those fingerprints.

How to add fingerprint



来源:https://stackoverflow.com/questions/48536142/firebase-not-sending-otp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!