5

I have an issue where I am using google sign in for my application + Firebase.

Suppose there are 3 users X,Y and Z with whom I am login in my application at one instance of time.

My code for checking the user is logged in or not:

FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser currentUser = mAuth.getCurrentUser();

if(currentUser!=null)
   {
     //user logged in and get user detail from currentUser and go to HomePage
   }
 else{
    //Show sign in button
   }

Scenario : I signed it with multiple accounts and logged out. everything working fine.

But when I delete the application and again install it. The Y user is automatically return by mAuth.getCurrentUser().

I tried login with X and Uninstall the application. Still, if I install it again it directly takes me to Y user.

I tried login with Z and Uninstall the application. Still, if I install it again it directly takes me to Y user.

I even tried login out and Delete the application. Again if I install, it returns Y user without even asking for sign in.

NOTE : I havent cleared cache or data . If I do that everything works fine.

The issue is just unistalling the app.

What must be the reason ?

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Abubakker Moallim
  • 1,610
  • 10
  • 20
  • _tried login with X and Uninstall the application_ where you able to login with X or was Y logged in? – Peter Haddad Feb 07 '18 at 09:41
  • Again Y is returning. In whatever case if I uninstall the app and install it returns Y and I am really fumbled – Abubakker Moallim Feb 07 '18 at 09:43
  • What about this `android:allowBackup="false"` in application tag – Sahdeep Singh Feb 07 '18 at 09:51
  • Can you explain for what it is ? But I also know what to know the reason behind that Why only ``Y`` is returning – Abubakker Moallim Feb 07 '18 at 09:53
  • 1
    Firebase keeps some data even after uninstalling, to delete data after uninstalling use these `android:allowBackup="false"` `android:fullBackupContent="false" ` . And i am not sure why only Y but you can try using this and after that test again by signing with X or Z without using these tags – Sahdeep Singh Feb 07 '18 at 10:04

1 Answers1

5

From the docs:

When a user signs up or signs in, that user becomes the current user of the Auth instance. The Firebase Auth instance persists the user's state, so that refreshing the page (in a browser) or restarting the application doesn't lose the user's information.

When the user signs out, the Auth instance stops keeping a reference to the User object and no longer persists its state; there is no current user. However, the user instance continues to be completely functional: if you keep a reference to it, you can still access and update the user's data.

So to solve this, the best way is to create a button and sign out the user. That way that user won't be logged in when you restart the application.

FirebaseAuth.getInstance().signOut();

More info here: https://firebase.google.com/docs/auth/users

Also this question related to ios (but same idea): Firebase - Deleting and reinstalling app does not un-authenticate a user

Some other alternatives also:

adding android:allowBackup="false" in your <application> in manifest.

android:allowBackup

Whether to allow the application to participate in the backup and restore infrastructure. If this attribute is set to false, no backup or restore of the application will ever be performed, even by a full-system backup that would otherwise cause all application data to be saved via adb. The default value of this attribute is true.

Do this as a Test:

  1. Delete Cache and Data
  2. Login with User Y and Logout
  3. Login with User X and Logout
  4. Uninstall the application
  5. Install the application login with user X.

It is important to have FirebaseAuth.getInstance().signOut(); when logging out.

Community
  • 1
  • 1
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134