Android FirebaseAuth.getCurrentUser()永不为空 [英] Android FirebaseAuth.getCurrentUser() Never Null

查看:43
本文介绍了Android FirebaseAuth.getCurrentUser()永不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DispatchActivity作为启动器活动,用于检查当前是否有用户登录.如果该用户已登录,则将其发送到他们的ProfileActivity.否则,我将它们发送到LogInActivity.这是我在DispatchActivity中的代码:

I have a DispatchActivity as my Launcher Activity, which is meant to check if there is a user currently signed in. If the user is signed in, I send them to their ProfileActivity. Otherwise, I send them to a LogInActivity. Here is my code in DispatchActivity:

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

    //.....................

    auth = FirebaseAuth.getInstance();

    authListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                launchProfileActivity();
            } else {
                // User is signed out
                launchLoginActivity();
            }
        }
    };
}

@Override
public void onStart() {
    super.onStart();
    auth.addAuthStateListener(authListener);
}

@Override
public void onStop() {
    super.onStop();
    if (authListener != null) {
       auth.removeAuthStateListener(authListener);
    }
}

无论我做什么,firebaseAuth.getCurrentUser()都不会在我的主要测试设备上返回null.根据文档,除非当前有一个用户登录,否则getCurrentUser()将为null.当我记录该用户的某些详细信息时,它们与我之前创建的测试"用户一致(即,调用user.getEmail()返回"email@example.com".

No matter what I do, firebaseAuth.getCurrentUser(), never returns null on my primary testing device. According to the Docs, getCurrentUser() will be null unless there is a User Currently Signed in. When I Log some of this User's details, they are consistent with a "Test" user I created previously, (i.e. calling user.getEmail() returns "email@example.com".

这是有问题的,因为我很肯定在控制台的用户数据库(在Authentication/Users中)没有任何用户.我前一段时间从数据库中删除了该测试用户,并且数据库为空.

This is problematic, as I most certainly don't have any users in my Console's User Database (In Authentication/Users). I deleted this test User from the database some time ago, and the database is empty.

我尝试在另一台设备上运行该应用程序,并且该应用程序正常运行.但是,在我的主要测试设备上执行全新安装不能解决该问题.

I tried running the App on another device, and it executed properly. However, performing a fresh install on my primary testing device does not fix the problem.

问题: 据我所知,该问题与设备的某种持久性用户状态有关.因为运行其他设备可以正常工作.由于我没有故意配置这种情况的发生,因此我想知道是什么导致了我的Auth用户数据库和设备之间的这种不一致.

Question: As far as I can see, the issue is related to some kind of persistent user state with my device; since running a different device works fine. Since I haven't deliberately configured this to occur, I would like to know what is causing this inconsistency between my Auth User Database and the Device.

如果数据库为空,那么auth.getCurrentUser()从何处获取以前删除的用户对象?

谢谢.

推荐答案

Firebase身份验证将在用户登录时为用户缓存身份验证令牌.这避免了必须对用户与由其提供的其他服务进行的每一次微小交互进行身份验证的情况.火力基地.该令牌会定期自动刷新,但在此之前,SDK会假定该令牌代表用户.您会发现令牌会在一段时间后过期,或者您可以通过卸载并重新安装该应用程序或清除其数据来强制该问题.

Firebase Authentication will cache an authentication token for the user when they're signed in. This prevents having to authenticate every little interaction the user makes with other services provided by Firebase. This token gets automatically refreshed periodically, but until then, the SDK assumes that the token represents the user. You'll find that the token will expire after some time, or you can force the issue by uninstalling and reinstalling the app, or clearing its data.

身份验证有效,无论实时数据库的内容如何.它们彼此独立,除非安全规则将访问权限限制为当前经过身份验证的用户.

Authentication works regardless of the contents of your Realtime Database. They are independent from each other, except where security rules limit access to the currently authenticated user.

这篇关于Android FirebaseAuth.getCurrentUser()永不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆