简体   繁体   English

使用短信验证的 Firebase 身份验证无法正常工作

[英]Firebase Authentication using Sms verification is not working properly

I used Firebase Authentication using Phone number in My android application.我在我的 android 应用程序中使用电话号码使用 Firebase 身份验证。

The authentication works fine when you enter the number for the first time and it can automatically detect the OTP if the app is opened in the same android device the provided phone number is given.当您第一次输入号码时,身份验证工作正常,如果应用程序在提供电话号码的同一 Android 设备中打开,它可以自动检测 OTP。

But when I enter the mobile number again it does not send the verification sms containing OTP.但是当我再次输入手机号码时,它不会发送包含 OTP 的验证短信。

here is the class where the phone number is provided.这是提供电话号码的课程。

public class LoginActivity extends AppCompatActivity {

    private EditText LoginPhone;
    private Button LoginConfirmButton;

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

        LoginPhone = (EditText) findViewById(R.id.PhoneNumberLogin);
        LoginConfirmButton = (Button) findViewById(R.id.Loginbutton);
        Button registerbutton=(Button)findViewById(R.id.RegisterButton);

        LoginConfirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String number=LoginPhone.getText().toString();
                if(number.isEmpty() || number.length()<11)
                {
                    LoginPhone.setError("Valied Number Required");
                    LoginPhone.requestFocus();
                }

                String PhoneNumber="+88"+number;
                Intent firstintent=new Intent(LoginActivity.this,CodeConfirm.class);
                firstintent.putExtra("PhoneNumber",PhoneNumber);
                startActivity(firstintent);
            }
        });

        registerbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this,User_Registration.class));
            }
        });
    }
}

And Here is the Class where The user can put the OTP manually.这是用户可以手动放置 OTP 的类。

public class CodeConfirm extends AppCompatActivity {

    EditText Otpverify;
    int s;
    private String VerificationCode;
    Button confirmbutton;
    private FirebaseAuth mAuth;

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

        Otpverify = (EditText) findViewById(R.id.ConfirmCode);
        mAuth= FirebaseAuth.getInstance();
        String PhoneNumber = getIntent().getStringExtra("PhoneNumber");

        SendVerificationCode(PhoneNumber);

        confirmbutton=(Button)findViewById(R.id.ConfirmButton);

        confirmbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String code=Otpverify.getText().toString().trim();

                if(code.isEmpty() || code.length()<6)
                {
                    Otpverify.setError("Enter the OTP properly");
                    Otpverify.requestFocus();
                    return;
                }
                Verifycode(code);
            }
        });
    }

    private void Verifycode(String code) {
        PhoneAuthCredential credential=PhoneAuthProvider.getCredential(VerificationCode,code);
        Signin(credential);

    }

    private void Signin(PhoneAuthCredential credential) {

        mAuth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {

                if(task.isSuccessful())
                {
                    Intent WorkingSwitch=new Intent(CodeConfirm.this,Current_Location.class);
                    WorkingSwitch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(WorkingSwitch);
                }
            }
        });
    }

    private void SendVerificationCode(String Number) {

        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                Number,
                60,
                TimeUnit.SECONDS,
                TaskExecutors.MAIN_THREAD,
                mCallBack
        );
    }

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

        @Override
        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {

            String code=phoneAuthCredential.getSmsCode();
            if(code!=null)
            {
                Verifycode(code);
            }
        }

        @Override
        public void onVerificationFailed(FirebaseException e) {

            Toast.makeText(CodeConfirm.this,e.getMessage(),Toast.LENGTH_LONG).show();

        }

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

I want to send the OTP to the phone number every time the number is provided even when if same number is provided multiple times.即使多次提供相同的号码,我也想在每次提供号码时将 OTP 发送到电话号码。

The verifyPhoneNumber method does not send an OTP if the number has been recently verified, if a verification is already in progress for the number, or if too many verification request were sent to the number recently.如果该号码最近已被验证过,如果该号码的验证已经在进行中,或者如果最近向该号码发送了太多验证请求,则verifyPhoneNumber方法不会发送 OTP。

Also note this from the documentation :还要注意文档中的这一点:

To prevent abuse, Firebase enforces a limit on the number of SMS messages that can be sent to a single phone number within a period of time.为防止滥用,Firebase 对一段时间内可以发送到单个电话号码的 SMS 消息数量实施了限制。 If you exceed this limit, phone number verification requests might be throttled.如果超过此限制,电话号码验证请求可能会受到限制。 If you encounter this issue during development, use a different phone number for testing, or try the request again later.如果您在开发过程中遇到此问题,请使用其他电话号码进行测试,或稍后重试该请求。

If you want to force it to send a new OTP, use the verifyPhoneNumber overload that takes a forceResendingToken parameter which you get from the previous onCodeSent .如果您想强制它发送新的 OTP,请使用verifyPhoneNumber重载,重载采用您从之前的onCodeSent获得forceResendingToken参数

If you want to send to the same number for testing, whitelisting the number is a better option.如果您想发送到同一号码进行测试, 将该号码列入白名单是更好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM