简体   繁体   English

使用 onCompleteListener 进行 Firebase 身份验证不起作用

[英]Firebase authentication with onCompleteListener not working

I am trying to implement a basic android authentication system with Firebase.我正在尝试使用 Firebase 实现一个基本的 android 身份验证系统。 The following is my code:以下是我的代码:

Toast.makeText(MainActivity.this, "BEFORE ON-COMPLETE", Toast.LENGTH_SHORT).show();  //this shows up
    mAuth.signInWithEmailAndPassword(emailInputted, passInputted).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            Toast.makeText(MainActivity.this, "in OnComplete", Toast.LENGTH_SHORT).show(); //this does not show up
            if(task.isSuccessful()){
                Intent i = new Intent(MainActivity.this, Dashboard.class);
                i.putExtra("EMAIL", emailInputted);
                Toast.makeText(MainActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
                startActivity(i);
            } else {
                Toast.makeText(MainActivity.this, "Please check email/password", Toast.LENGTH_SHORT).show();
            }
        }
    });
    Toast.makeText(MainActivity.this, "after OnComplete", Toast.LENGTH_SHORT).show(); //this shows up

I took this code from the official Firebase docs , but for some reason it is not working.我从Firebase官方文档中获取了这段代码,但由于某种原因它不起作用。 The onComplete method is not being called for some reason.由于某种原因没有调用onComplete方法。 I can see the "before on-Complete" and "after on-complete" toasts, but not the "in OnComplete" one.我可以看到“完成前”和“完成后”祝酒词,但看不到“在 OnComplete 中”的祝酒词。 I have added the users in my authentication table in the firebase console, and I am positive that I am entering the correct password.我已经在 firebase 控制台的身份验证表中添加了用户,并且我确定我输入了正确的密码。

After using the debugger, I saw that my code just skips over the onComplete() method, and does not even get to the isSuccessful() method.使用调试器后,我看到我的代码只是跳过了onComplete()方法,甚至没有到达isSuccessful()方法。 How do I fix this?我该如何解决?

Thanks in advance!提前致谢!

Follow this, I hop you will get your answer.按照这个,我希望你会得到你的答案。

lBut = findViewById(R.id.log_btn);
        lBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String a = usrn.getText().toString().trim();
                String b = pswd.getText().toString().trim();

                if (TextUtils.isEmpty(a) && TextUtils.isEmpty(b)) {
                    usrn.setError("Empty Email !!");
                    pswd.setError("Empty Password !!");

                    lBut.setClickable(false);
                } else {
                    lBut.setClickable(true);
                    mAuth.signInWithEmailAndPassword(a, b).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @SuppressLint("ShowToast")
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                Toast.makeText(getApplicationContext(), "Successfully Login!!", Toast.LENGTH_SHORT).show();
                                Intent i = new Intent(getApplicationContext(), HomeP.class);
                                ProgressBar prb = findViewById(R.id.progressBar);
                                prb.setVisibility(View.VISIBLE);
                                startActivity(i);
                                finish();
                            } else
                                Toast.makeText(getApplicationContext(), "Check Error And Try Again!!", Toast.LENGTH_SHORT).show();
                        }
                    });

                }
            }
        });

This is the Event on the Login Button, you have more doubt then reply me.这是登录按钮上的事件,您有更多疑问然后回复我。

So it fixed itself after I restarted my computer.所以它在我重新启动计算机后自行修复。 No code changes.没有代码更改。 Maybe Android Studio was being wacky.也许 Android Studio 很古怪。

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

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