Android FirebaseAuth.getCurrentUser () Never Null

I have DispatchActivity as my Launcher activity, which is designed to check if there is a user who is currently logged in. If the user is registered, I submit it to my ProfileActivity. Otherwise, I submit them to 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); } } 

No matter what I do, firebaseAuth.getCurrentUser () never returns null on my main test device. According to Docs, getCurrentUser () will be null unless the user is logged in. When I register some data of this user, they are consistent with the user "Test" that I created earlier (for example, calling user.getEmail () returns " email@example.com ".

This is problematic, since I certainly do not have users in my console user database (In Authentication / Users). I removed this User test from the database a while ago, and the database is empty .

I tried to run the application on another device and executed it correctly. However, when I perform a new installation on my primary test device, it does not complete the problem.

Question: As far as I can see, the problem is related to some constant state of the user with my device; since another device is working, it’s working fine. Since I did not intentionally configure this, I would like to know what causes this inconsistency between my Auth User Database and Device.

If the database is empty, where does auth.getCurrentUser () get this previously deleted user object from?

Thanks.

+5
source share
2 answers

Firebase Authentication will cache the authentication token for the user when it is signed. This prevents the authentication of every small interaction the user makes with other Firebase services. This token is automatically updated, but before that, the SDK assumes that the token represents the user. You will find that the token expires after a while or you can forcefully fix the problem by uninstalling and reinstalling the application, or by clearing its data.

Authentication works regardless of the contents of your real-time database. They are independent of each other, unless security rules restrict access to an authenticated user.

+3
source

I had the exact same question and could not find the answer to this problem anywhere. I tried everything. I don’t know why this works, but I restarted my phone and everything happened to fix it. Super late, but hope this helps someone else with the same problem.

0
source

Source: https://habr.com/ru/post/1263179/


All Articles