使用FirebaseAuth进行验证时未获得OTP

时间:2019-05-24 09:32:36

标签: android firebase firebase-authentication android-developer-api

private ImageView back, logo;
private EditText orgname, urname, desig, email, phnmb;
private TextView goback;
private Button creacc;
private Organization org;

private String organame, yourname, desg, emailid, numb, mVerificationId;
String passotp="";
private FirebaseAuth mAuth;
private FirebaseDatabase database;
private DatabaseReference myRef;
private FirebaseAuth.AuthStateListener mAuthListener;
String TAG = getClass().getName();


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

    back = (ImageView) findViewById(R.id.img_back);
    logo = (ImageView) findViewById(R.id.img_logo);
    orgname = (EditText) findViewById(R.id.edit_orgname);
    urname = (EditText) findViewById(R.id.edit_yourname);
    desig = (EditText) findViewById(R.id.edit_designation);
    email = (EditText) findViewById(R.id.edit_emailid);
    phnmb = (EditText) findViewById(R.id.edit_phone);
    creacc = (Button) findViewById(R.id.but_createacc);
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    mAuth = FirebaseAuth.getInstance();


    creacc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            organame = orgname.getText().toString();
            yourname = urname.getText().toString();
            desg = desig.getText().toString();
            emailid = email.getText().toString();
            numb = phnmb.getText().toString();


            if (orgname.getText().toString().trim().equalsIgnoreCase("")) {
                orgname.setError("This field can not be blank");
            }

            if (urname.getText().toString().trim().equalsIgnoreCase("")) {
                urname.setError("   This field can not be blank");
            }
            if (desig.getText().toString().trim().equalsIgnoreCase("")) {
                desig.setError("This field can not be blank");
            }
            if (email.getText().toString().trim().equalsIgnoreCase("")) {
                email.setError("This field can not be blank");
            }
            if (phnmb.getText().toString().trim().equalsIgnoreCase("")) {
                phnmb.setError("This field can not be blank");
            }
            phnmb.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {


                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (s.length() < 10) {
                        phnmb.setError("Should contain 10 number");


                    }
                }
            });
            //Toast.makeText(getApplicationContext(), "Name" + organame + "UrName" + yourname + "Des" + desg + "Email" + emailid + "numb" + numb, Toast.LENGTH_SHORT).show();

            if ((!organame.isEmpty() && organame != null) & (!yourname.isEmpty() && yourname != null) & (!desg.isEmpty() && desg != null) & (!emailid.isEmpty() && emailid != null) & (!numb.isEmpty() && numb != null&&numb.length()==10)) {
                myRef = FirebaseDatabase.getInstance().getReference(organame);
                //String user_id=mAuth.getCurrentUser().getUid();
                //DatabaseReference current_user_db= myRef.child("Organization");
                myRef.child("OrgName").setValue(organame);
                myRef.child("Name").setValue(yourname);
                myRef.child("Designation").setValue(desg);
                myRef.child("Email Id").setValue(emailid);
                myRef.child("Ph Numb").setValue(numb).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            //verifyPhoneNum(numb);
                            Toast.makeText(getApplicationContext(), "Task Successful "+numb, Toast.LENGTH_SHORT).show();
                            Intent startotp = new Intent(getApplicationContext(),Otp.class);
                            startotp.putExtra("mobile",numb);
                            startActivity(startotp);

                        } else {
                            Log.d(TAG, task.getException().toString());
                            Toast.makeText(getApplicationContext(), "Exception" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        }

                    }
                });
            }
        }
    });

}

这是我的验证活动:  私人Pinview引脚;     私人TextView t1,t2,验证;     私有字符串otpcode;`     意向意向;     专用String pinnumb;     私有字符串号;     私有字符串mVerificationId;     私有FirebaseAuth mAuth;     字符串TAG = getClass()。getName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_otp);
    pin = (Pinview) findViewById(R.id.pinview);
    t1 = (TextView) findViewById(R.id.text_verifycode);
    t2 = (TextView) findViewById(R.id.text_type);
    validate = (TextView) findViewById(R.id.text_validate);
    intent = getIntent();
     number = intent.getStringExtra("mobile");
    Log.d(TAG,number);
     Toast.makeText(getApplicationContext(),"Ph Number is :"+number,Toast.LENGTH_SHORT).show();
    mAuth = FirebaseAuth.getInstance();
    Log.d("Mauth is :",mAuth.toString());
    sendVerificationCode(number);


    pin.setPinViewEventListener(new Pinview.PinViewEventListener() {
        @Override
        public void onDataEntered(Pinview pinview, boolean fromUser) {
            //Make api calls here or what not
            Toast.makeText(getApplicationContext(), pinview.getValue(), Toast.LENGTH_SHORT).show();
            pinnumb = pinview.getValue();
        }
    });
    validate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent homepage =new Intent(getApplicationContext(),Navi_Organization.class);
                startActivity(homepage);
            }
    });

}

private void sendVerificationCode(String mobile) {
    Log.d(TAG,"I am in sendVerification Code "+mobile);
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            "+91 "+mobile,
            60,
            TimeUnit.SECONDS,
            TaskExecutors.MAIN_THREAD,
            mCallbacks);
}

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

        //Getting the code sent by SMS
        String code = phoneAuthCredential.getSmsCode();
        Log.d(TAG, "onVerificationCompleted:" + phoneAuthCredential);
        Toast.makeText(getApplicationContext(),"Code is "+code,Toast.LENGTH_SHORT).show();

        //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) {
            Toast.makeText(getApplicationContext(),"Code is "+code,Toast.LENGTH_SHORT).show();
            pin.setValue(code);
            //verifying the code
            verifyVerificationCode(code);
        }
    }

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

    @Override
    public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
        //super.onCodeSent(s, forceResendingToken);
        Log.d(TAG,"I am in Code Send");

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


private void verifyVerificationCode(String code) {
    //creating the credential
    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, code);

    //signing the user
    signInWithPhoneAuthCredential(credential);
}

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(Otp.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Toast.makeText(getApplicationContext(),"I am in phoneAuth Credential",Toast.LENGTH_SHORT).show();
                        //verification successful we will start the profile activity
                        Intent intent = new Intent(Otp.this, Navi_Organization.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);

                    } else {

                        //verification unsuccessful.. display an error message

                        String message = "Somthing is wrong, we will fix it soon...";
                        Toast.makeText(getApplicationContext(),"Error :"+message,Toast.LENGTH_SHORT).show();

                        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();
                    }
                }
            });
}

}

我没有获得OTP号码,谁能告诉我我犯了什么错误。

0 个答案:

没有答案