问题
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 ?
回答1:
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:
- Delete Cache and Data
- Login with User Y and Logout
- Login with User X and Logout
- Uninstall the application
- Install the application login with user X.
It is important to have FirebaseAuth.getInstance().signOut();
when logging out.
来源:https://stackoverflow.com/questions/48660300/firebase-authentication-returning-specific-user-when-app-is-uninstalled-and-inst