【问题标题】:Manually verify firebase otp手动验证 Firebase otp
【发布时间】:2019-12-07 17:18:23
【问题描述】:

我正在寻找一种方法来查找从 firebase 电话身份验证发送给我的代码,因为我想手动验证该代码。现在问题是 firebase 正在自动检测短信并调用 onVerificationCompleted() 但我有一个按钮,我想手动输入 otp 代码并验证。下面是我的代码。任何帮助将不胜感激。谢谢

PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phonenumber,
                120,
                TimeUnit.SECONDS,
                this, new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
                Intent intent = new Intent(PhoneVerification.this, PictureActivity.class);
                Log.e("iamhere","Credential  IS"+phoneAuthCredential);
                intent.putExtra("email",email);
                intent.putExtra("phoneNumber",phonenumber);
                intent.putExtra("password",password);
                startActivity(intent);
                Toast.makeText(PhoneVerification.this, "Please fill the registration form", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onVerificationFailed(@NonNull FirebaseException e) {
                Toast.makeText(PhoneVerification.this, "Failed: "+e.getMessage(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                super.onCodeSent(s, forceResendingToken);
                Toast.makeText(PhoneVerification.this, "Check your phone for verification code", Toast.LENGTH_LONG).show();
                String mVerificationId = s;
            }
        });

【问题讨论】:

  • 我认为这是唯一的验证方法。但是为什么您要手动验证
  • 假设我输入了一个手机号码并且该 SIM 卡在另一个手机中,在这种情况下它无法自动验证代码,我必须手动输入该代码
  • 您是否可以通过手动输入代码来手动验证它
  • 我没有任何字符串代码,我可以检查它是否等于输入的代码

标签: android firebase one-time-password


【解决方案1】:

如果代码没有被自动检测到,用户必须通过EditText手动输入

    loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String txt = otpEditText.getText().toString().trim();
                if (txt.isEmpty() || txt.length() < 6) {
                    otp.setError("Enter valid code");

                    return;
                }

                //verifying the code entered manually
                if (isOnline()) {
                    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, txt);

                    signInWithPhoneAuthCredential(credential);
                } else {
                    showSnack(relativeLayout, "Unable to connect! Check internet connection");
                }
            }
        });



private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(Verification.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        //verification successfull do next task
                    } else {

                        //verification unsuccessful.. display an error message


                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            Toast.makeText(Verification.this, "Incorrect OTP entered", Toast.LENGTH_LONG).show();

                        } else {
                            Toast.makeText(Verification.this, "Unable to verify please retry later", Toast.LENGTH_LONG).show();


                        }


                    }
                }
            });
}

这是自动验证:

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

        //Getting the code sent by SMS
        String code = phoneAuthCredential.getSmsCode();

        //sometime the code is not detected automatically
        //in this case the code will be null
        //so user has to manually enter the code
        if (code != null) {
            otpEditText.setText(code);
            //verifying the code
            verifyVerificationCode(code);
        }
    }

    @Override
    public void onVerificationFailed(FirebaseException e) {
        FancyToast.makeText(Verification.this, e.getMessage(), FancyToast.LENGTH_LONG, FancyToast.CONFUSING, false).show();
    }

    @Override
    public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
        super.onCodeSent(s, forceResendingToken);

        //storing the verification id that is sent to the user
        mVerificationId = s;
    }
};

查看Firebase documentation 了解更多信息。

【讨论】:

  • 请修正代码缩进 - 它看起来很丑。您链接到的文档可能是一个谷歌搜索结果,它重定向到一个不正确的链接。 (编辑:我帮助修复了链接,但请考虑粘贴直接链接。)
  • 如果用户已经登录或者还在注册怎么办?
猜你喜欢
  • 2022-12-28
  • 1970-01-01
  • 1970-01-01
  • 2021-04-07
  • 1970-01-01
  • 2018-08-08
  • 2022-06-11
  • 2022-07-16
  • 2020-05-10
相关资源
最近更新 更多