【问题标题】:Firebase Auth getCurrentUser() retrieves a deleted userFirebase Auth getCurrentUser() 检索已删除的用户
【发布时间】:2019-09-21 12:36:10
【问题描述】:

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

我一直在使用 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()signInWithEmailAndPassword() 方法分开,这使我在两个活动 onCreate() 方法中添加了这个 mAuth = FirebaseAuth.getInstance()。在我添加的登录活动中

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

}

【问题讨论】:

  • 您之前成功退出了吗?
  • 没有试过,但如果我清除应用程序缓存和东西,它会断开用户的连接。只是不会检测到他不再存在。
  • 嗯...我完全错过了。可能是我换东西的时候!我会看看它是怎么回事,但非常感谢你的所有帮助!!! @JeelVankhede
  • 乐于助人:)

标签: android firebase authentication firebase-authentication


【解决方案1】:

您可以收听 AuthStateListener 了解FirebaseUser 的状态变化。

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

了解更多here


文档指出:

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

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

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

【讨论】:

  • 非常感谢!我补充一下,看看能不能解决问题!
  • 我已经用你的回答解决了问题,然后决定将登录和注册分开进行 2 个活动。这样,我还将 createAccount() 与 signInWithEmailAndPassword() 方法分开,这使我在两个活动的 onCreate() 方法中都添加了这个“mAuth = FirebaseAuth.getInstance()”。在登录活动中,我添加了 mAuthListener = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser currentUser = mAuth.getCurrentUser(); ... } };但现在不起作用。我是忘记了什么还是无法做到这一点?
  • 能分享一下OP中的代码吗?我会调查一下并回复你。
  • 操作?对不起,我没有到达那里o.o'
【解决方案2】:

好像firebase在本地保存用户偏好尝试清除应用程序并再次登录检查此文档FIREBASE USER AUTH

【讨论】:

    猜你喜欢
    • 2017-09-07
    • 1970-01-01
    • 2020-09-02
    • 2022-01-21
    • 2019-12-06
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 2021-02-26
    相关资源
    最近更新 更多