首页 > 解决方案 > Firebase PhoneAuth Signout 后 Firebase 数据库侦听器无法正常工作

问题描述

我使用 Firebase 电话身份验证和实时数据库制作了一个 Android 应用程序。

我的要求是首先检查用户是否没有。是否在我的数据库中,然后将 OTP 发送给他。

我的数据库: 员工节点

我的代码:

rootRef= FirebaseDatabase.getInstance().getReference().child("Employees").child("mobno");

//---------------1----------------

getcodeBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("abcd","get code button clicked");
                if (!cugET.getText().toString().trim().isEmpty() && cugValidFlag==true) {
                    Log.d("abcd","entered no. is valid");
                    enteredcugno = cugET.getText().toString().trim();
                    Log.d("abcd","Entered cugno: "+enteredcugno);

                    rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            Log.d("abcd","rootRef listener reached");
                            if(dataSnapshot.child(enteredcugno).exists()){
                                Log.d("abcd","cugno exists in db");
                                startPhoneNumberVerification(cugET.getText().toString().trim());
                                mVerified = false;
                                otpTV.setVisibility(View.VISIBLE);
                                otpET.setVisibility(View.VISIBLE);
                                otpValTV.setVisibility(View.VISIBLE);
                                verifycodeBtn.setVisibility(View.VISIBLE);
                            }
                            else{
                                Log.d("abcd","cugno doesn't exists in db");
                                Toast.makeText(cugLogin.this,"No such CUG No. found",Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
                }
        }
    });

//------------------------2--------------------

private void startPhoneNumberVerification(String phoneNumber) {
    Log.d("abcd","startPhoneNumberVerification");
    // [START start_phone_auth]
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            "+91"+phoneNumber,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks);        // OnVerificationStateChangedCallbacks
    // [END start_phone_auth]

}

//--------------3--------------------

mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        @Override
        public void onVerificationCompleted(PhoneAuthCredential credential) {
            // This callback will be invoked in two situations:
            // 1 - Instant verification. In some cases the phone number can be instantly
            //     verified without needing to send or enter a verification code.
            // 2 - Auto-retrieval. On some devices Google Play services can automatically
            //     detect the incoming verification SMS and perform verificaiton without
            //     user action.
            Log.d("abcd", "onVerificationCompleted:" + credential);

            signInWithPhoneAuthCredential(credential);
        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            // This callback is invoked in an invalid request for verification is made,
            // for instance if the the phone number format is not valid.
            Log.w("abcd", "onVerificationFailed", e);

            if (e instanceof FirebaseAuthInvalidCredentialsException) {
                Log.d("abcd","verification failed cz of FirebaseAuthInvalidCredentialsException");
                Toast.makeText(cugLogin.this,"Verification Failed !! Invalied verification Code",Toast.LENGTH_SHORT).show();
            }
            else if (e instanceof FirebaseTooManyRequestsException) {
                Log.d("abcd","verification failed cz FirebaseTooManyRequestsException");
                Toast.makeText(cugLogin.this,"Verification Failed !! Too many request. Try after some time. ",Toast.LENGTH_SHORT);
            }

        }

        @Override
        public void onCodeSent(String verificationId,
                               PhoneAuthProvider.ForceResendingToken token) {
            // The SMS verification code has been sent to the provided phone number, we
            // now need to ask the user to enter the code and then construct a credential
            // by combining the code with a verification ID.
            Log.d("abcd", "onCodeSent:" + verificationId);

            // Save verification ID and resending token so we can use them later
            mVerificationId = verificationId;
            mResendToken = token;
        }
    };

//----------------4---------------

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    Log.d("abcd","signInWithPhoneAuthCredential reached");
    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
                        Log.d("abcd", "signInWithCredential:success");

                        FirebaseUser user = task.getResult().getUser();
                        mVerified = true;
                        timer.cancel();
                        timerTV.setVisibility(View.INVISIBLE);
                        cugET.setEnabled(false);
                        cugTV.setVisibility(View.INVISIBLE);
                        cugET.setVisibility(View.INVISIBLE);
                        getcodeBtn.setVisibility(View.INVISIBLE);
                        otpTV.setVisibility(View.INVISIBLE);
                        otpET.setVisibility(View.INVISIBLE);
                        otpValTV.setVisibility(View.INVISIBLE);
                        verifycodeBtn.setVisibility(View.INVISIBLE);
                        Intent intent = new Intent(cugLogin.this,Login.class);
                        intent.putExtra("cugnotoLogin",enteredcugno);
                        startActivity(intent);
                        Toast.makeText(cugLogin.this,"Successfully verified",Toast.LENGTH_SHORT).show();
                        // ...
                    } else {
                        // Sign in failed, display a message and update the UI
                        Log.w("abcd", "signInWithCredential:failure", task.getException());
                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            // The verification code entered was invalid
                            Toast.makeText(cugLogin.this,"Invalid OTP ! Please enter correct OTP",Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
}

我看到的最后一个日志是“输入 cugno:9876543210”

没有显示错误,我真的很困惑如何纠正它?

如果我删除 rootRef 侦听器,我的代码正在工作,但我想先检查数据库中的用户。另外我给出了 rootRef 的正确路径,那为什么它不起作用?

编辑

我让我的代码工作了,但我仍然有点困惑,我只是在 getcodeBtn 侦听器中移动了我的 rootRef 初始化。但是我已经在我的 onStart() 函数中有它,所以它也应该在那里初始化,如果有人知道它背后的原因,请告诉我。

最新编辑

现在,当我退出时,我的代码再次卡在 rootRef 侦听器中,它没有被传递给它。即使在关闭应用程序后,它也不会让我进入。同样的事情再次发生。这就像一夜之间刷新了一些东西,应用程序让我登录,当我退出时,我又被卡住了。为什么会这样?

标签: androidfirebasefirebase-realtime-databasefirebase-authentication

解决方案


我解决了,不是编码问题,而是逻辑错误。我只为经过身份验证的用户设置数据库规则,并首先检查我的数据库以让用户登录,所以当我注销时,代码进入循环,因为它无法访问数据库让用户登录。


推荐阅读