首页 > 解决方案 > Firebase Auth getCurrentUser() 检索已删除的用户

问题描述

您好,感谢您的宝贵时间。

我一直在使用 Firebase 开发一个 android 应用程序。

我已经设置了 Firebase 身份验证,用户可以使用电子邮件和密码注册并在电子邮件验证后登录。

当应用程序打开时,我检查用户是否仍然使用该onStart()方法登录,但我在从 firebase 删除用户后尝试了,我仍然可以登录!

我应该以另一种方式检查吗?

@Override
public void onStart() {
    super.onStart();

    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = mAuth.getCurrentUser();
    updateUI(currentUser);
}

******************************************* 更新 ****** ******************************************

使用AuthStateListener修复了问题 ,但随后我将 signIn 和 createAccount 方法拆分为 2 个单独的活动,这样我还分离了createAccount()fromsignInWithEmailAndPassword()方法,这使我mAuth = FirebaseAuth.getInstance()在两个活动onCreate()方法中都添加了这个。在我添加的登录活动中

mAuthListener = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser currentUser = mAuth.getCurrentUser(); ... } };

但现在不起作用。我是忘记了什么还是无法做到这一点?

这是我发现相关的代码:

LogInActivity 类 onCreate():

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

    // Views
    emailEditText = findViewById(R.id.emailEditText);
    passwordEditText = findViewById(R.id.pswdEditText);

    // Buttons
    findViewById(R.id.logInButton).setOnClickListener(this);
    findViewById(R.id.forgottenPaswdTextButton).setOnClickListener(this);
    findViewById(R.id.registerTextButton).setOnClickListener(this);

    // Initialize Firebase Auth
    mAuth = FirebaseAuth.getInstance();

    // Check for user connection
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            // Check if user is signed in (non-null) and update UI accordingly.
            FirebaseUser currentUser = mAuth.getCurrentUser();
            if (currentUser != null) {
                Log.d(TAG, "onAuthStateChanged:signed_in:" + currentUser.getUid());
            } else {
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
            updateUI(currentUser);
        }
    };
}

SignInActivity 类 onCreate():

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

    // Views
    emailEditText = findViewById(R.id.emailEditText);
    passwordEditText = findViewById(R.id.pswdEditText);
    passwordRetypedEditText = findViewById(R.id.pswdRetypeEditText);
    nameEditText = findViewById(R.id.nameEditText);

    // Buttons
    findViewById(R.id.signUpButton).setOnClickListener(this);
    findViewById(R.id.logInTextButton).setOnClickListener(this);

    // Initialize Firebase Auth
    mAuth = FirebaseAuth.getInstance();

}

标签: androidfirebaseauthenticationfirebase-authentication

解决方案


您可以收听AuthStateListenerFirebaseUser状态变化。

为什么?,基本上当您从服务器或其他设备中删除用户时,它在您当前的设备中仍然有效,因为它的令牌尚未在本地刷新。

在此处了解更多信息


文档指出:

在某些情况下,getCurrentUser将返回非 null FirebaseUser但基础令牌无效。例如,如果用户在另一台设备上被删除并且本地令牌未刷新,则可能会发生这种情况。在这种情况下,您可能会获得一个有效用户getCurrentUser,但随后对经过身份验证的资源的调用将失败。

getCurrentUser也可能返回null,因为 auth 对象尚未完成初始化。

如果您附加一个,则每次底层令牌状态更改时AuthStateListener都会收到回调。这对于对上述边缘情况做出反应很有用。


推荐阅读