如果已通过OTP Firebase验证,则无法再次登录

时间:2019-05-03 16:55:02

标签: android firebase firebase-authentication

我使用电话号码创建了一个Firebase身份验证演示。我能够使用OTP成功验证。但是,当我尝试再次输入该数字时,不会出现OTP。我卸载了该应用程序,然后再次尝试验证手机号码,但发生了同样的事情,Firebase没有OTP。我不知道该怎么做才能再次获得注册号上的OTP。

这是我的验证OTP的代码:

public class VerifyOTP extends AppCompatActivity {

private String mVerificationId;
private EditText editTextCode;
private FirebaseAuth mAuth;

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

    mAuth = FirebaseAuth.getInstance();
    editTextCode = findViewById(R.id.editTextCode);

    Intent intent = getIntent();
    String mobile = intent.getStringExtra("mobile");
    sendVerificationCode(mobile);

    findViewById(R.id.buttonSignIn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String code = editTextCode.getText().toString().trim();
            if (code.isEmpty() || code.length() < 6) {
                editTextCode.setError("Enter valid code");
                editTextCode.requestFocus();
                return;
            }

            verifyVerificationCode(code);
        }
    });

}

private void sendVerificationCode(String mobile) {
    PhoneAuthProvider.getInstance().verifyPhoneNumber("+91" + mobile,
            60, TimeUnit.SECONDS,
            this,
            mCallbacks);

}

private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
    @Override
    public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {

        String code = phoneAuthCredential.getSmsCode();

        if (code != null) {
            editTextCode.setText(code);
            verifyVerificationCode(code);
        }
    }

    @Override
    public void onVerificationFailed(FirebaseException e) {
        Toast.makeText(VerifyOTP.this, e.getMessage(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
        super.onCodeSent(verificationId, forceResendingToken);
        Log.d("Oncode sent",  verificationId);
        mVerificationId = verificationId;

    }
};


private void verifyVerificationCode(String code) {

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

    signInWithPhoneAuthCredential(credential);
}

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(VerifyOTP.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Toast.makeText(VerifyOTP.this, "Success", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(VerifyOTP.this, "Error", Toast.LENGTH_SHORT).show();
                        String message = "Somthing is wrong, we will fix it soon...";

                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            message = "Invalid code entered...";
                        }

                        Snackbar snackbar = Snackbar.make(findViewById(R.id.parent), message, Snackbar.LENGTH_LONG);
                        snackbar.setAction("Dismiss", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        });
                        snackbar.show();
                    }
                }
            });
}

}

0 个答案:

没有答案