我使用OTP进行的Firebase身份验证无法正常工作

时间:2019-03-10 05:01:56

标签: android firebase-authentication

我有一个登录活动,用户可以从移动设备使用OTP登录,为此我使用了Firebase。它以前曾经工作过,但是现在发生了一些事情,我无法弄清楚。请帮我。可能是从文本视图读取电话号码的问题,我也安装了sh1指纹。

这是代码:

public class LoginActivity extends AppCompatActivity {
EditText phonetextview;
String p;
FirebaseAuth auth;
String codesent;
String otp;
FirebaseAuth mAuth;
    private PhoneAuthProvider.ForceResendingToken mResendToken;







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









    mAuth = FirebaseAuth.getInstance();












    Button button = (Button) findViewById(R.id.button2);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {




            EditText fd = (EditText) findViewById(R.id.et1);
            String value= fd.getText().toString();
      //        int finalValue=Integer.parseInt(value);

            final EditText sd = (EditText) findViewById(R.id.et2);
            String value1= sd.getText().toString();
            // int finalValue1=Integer.parseInt(value1);

            EditText td = (EditText) findViewById(R.id.et3);
            String value2= td.getText().toString();
            // int finalValue2=Integer.parseInt(value2);

            EditText fod = (EditText) findViewById(R.id.et4);
            String value3= fod.getText().toString();
            //int finalValue3=Integer.parseInt(value3);

            EditText fid = (EditText) findViewById(R.id.et5);
            String value4= fid.getText().toString();
            //int finalValue4=Integer.parseInt(value4);

            EditText sid = (EditText) findViewById(R.id.et6);
            String value5= sid.getText().toString();
            //int finalValue5=Integer.parseInt(value5);


            otp = value + value1+value2+value3+value4+value5;
            Toast.makeText(getBaseContext(),otp,Toast.LENGTH_SHORT).show();
            verify();


        }
    });



    CardView card_view = findViewById(R.id.cardView);
    card_view.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {








            send();


          //  Toast toast = Toast.makeText(getApplicationContext(), phone_number,
            //        Toast.LENGTH_SHORT);
            //toast.show();



        }


    });

}

private void send() {

    phonetextview =findViewById(R.id.phonetextview);
          String p = phonetextview.getText().toString();
   // phone_number = PhoneNumberUtils.formatNumber(phonetextview.getText().toString());
    //int p= Integer.parseInt(phone_number) ;
    //int abc = Integer.parseInt(phonetextview.getText().toString());

    PhoneAuthProvider.getInstance().verifyPhoneNumber(
           "+91"+p,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks);             // ForceResendingToken from callbacks
              Toast toast = Toast.makeText(getApplicationContext(),p,
                     Toast.LENGTH_SHORT);
                                         toast.show();
}

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

        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            Toast.makeText(getBaseContext(),"faild",Toast.LENGTH_SHORT).show();
        }

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

            Toast.makeText(getBaseContext(),"jjj",Toast.LENGTH_LONG).show();

        }

    };






    private void verify()
    {

        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codesent, otp);
        signInWithPhoneAuthCredential(credential);

    }
    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information

                            Toast.makeText(getBaseContext(),"success",Toast.LENGTH_SHORT).show();
                            FirebaseUser user = task.getResult().getUser();

                        } else {
                            Toast.makeText(getBaseContext(),"ffffffffffff",Toast.LENGTH_SHORT).show();
                            // Sign in failed, display a message and update the UI

                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                                // The verification code entered was invalid
                            }
                        }
                    }
                });
    }

}

0 个答案:

没有答案