【问题标题】:firebase user is returning null everytime after it is called for the firsttimefirebase 用户在第一次调用后每次都返回 null
【发布时间】:2019-08-28 18:48:45
【问题描述】:

第一次调用此 (user = firebaseAuth.getCurrentUser();) 后,Firebase 用户返回 null。

当应用程序第一次安装时,firebaseAuth.getCurrentUser() 正在返回用户,但是当它从第一个活动移动到另一个活动并且在 firebaseAuth.getCurrentUser() 每次都返回 null 之后调用 sigh Out 按钮时。

package com.example.firebaselogin;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class logIn extends AppCompatActivity {

    EditText userName, pwd;
    TextView signUpHere;
    Button loginButton;
    private FirebaseAuth firebaseAuth;
    //private FirebaseAuth.AuthStateListener listener;

    ProgressDialog progressDialog;
    FirebaseUser user;

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

        firebaseAuth = FirebaseAuth.getInstance();
        progressDialog = new ProgressDialog(this);

        user = firebaseAuth.getCurrentUser();

        userName = findViewById(R.id.userName);
        pwd = findViewById(R.id.pwd);
        signUpHere = findViewById(R.id.signUpHere);
        loginButton = findViewById(R.id.loginButton);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                progressDialog.setMessage("Please Wait");
                progressDialog.show();

                Log.i("Userrrr", String.valueOf(user));

                if(validate()) {
                    if (user != null) {
                        firebaseAuth.signInWithEmailAndPassword(userName.getText().toString().trim(), pwd.getText().toString().trim()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (task.isSuccessful()) {
                                    Intent intent = new Intent(logIn.this, WelcomePage.class);
                                    startActivity(intent);
                                    Toast.makeText(getApplicationContext(), "Successful", Toast.LENGTH_SHORT).show();
                                    progressDialog.dismiss();
                                } else {
                                    Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
                                    progressDialog.dismiss();
                                }
                            }
                        });
                    } else {
                        Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
                        progressDialog.dismiss();
                    }
                }
            }
        });

        signUpHere.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
            }
        });

    }

    private Boolean validate() {

        Boolean result = false;

        String name = userName.getText().toString();
        String password = pwd.getText().toString();

        if(name.isEmpty() || password.isEmpty()) {
            Toast.makeText(getApplicationContext(), "Enter all details", Toast.LENGTH_SHORT).show();
        } else {
            result = true;
        }

        return result;

    }

}

firebaseAuth.getCurrentUser()第一次被调用时,它是

返回 (2019-08-29 00:08:14.561 8165-8165/com.example.firebaselogin I/Userrrr: com.google.firebase.auth.internal.zzk@44ffb65) 但之后 signOut 是从下一个活动触发的, firebaseAuth.getCurrentUser() 每次都返回 null 2019-08-29 00:08:24.651 8165-8165/com.example.firebaselogin I/Userrrr: null``

【问题讨论】:

  • 当signout被调用时,它会注销用户会话。所以不会有用户,它显然返回null
  • 但进入登录活动后无法再次登录。调用注销后如何获取用户?
  • 刚刚删除 if(user != null) {} line...

标签: java android firebase firebase-authentication


【解决方案1】:

正如我所见,您在签署用户之前检查用户是否为空。因此,当用户注销并尝试再次登录时,现在用户为空。根据您的情况,如果用户不为null,则全部登录,否则不会。由于用户为空,它总是失败并且用户无法登录。删除该条件,您就可以开始了。

【讨论】:

  • 是的..现在我的问题是我将如何在注销后重新登录?
  • 好的。告诉我你有什么活动和活动系列。我会帮助你并提供退出按钮的代码
  • 我发现我的错误现在已经解决了,非常感谢@Thrishool MSM
【解决方案2】:

每当您退出用户并调用代码时

FirebaseAuth.getInstance().signOut();

它将清除由 firebase 维护的会话。但是如果signOut() 没有被调用,那么会话仍然存在并且用户不会为空。所以每当你打电话给signOut(),你都会得到用户null。因此,只需删除 user != null 条件,您就可以登录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 2016-04-13
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    相关资源
    最近更新 更多