My login system uses SharedPreferences to store information about the user. The main two variables for the login system are:
loggedin - boolean
userID - int of the userID that's logged in (primary key on the DB)
When the user logs in, loggedin is set to 1 and the userID is set to the user ID fetched from the DB. When the logout button is pressed loggedin is set to 0 and userID set to null.
Situations:
I log in, close the app and repoen it = fine, still logged in to the correct account
I log in, then logout, close & reopen the app = fine, comes up with login screen
I carry out invalidate caches and restart in android studio = fine, stays logged in on the correct account.
Now here's the one that's going wrong: If I login, then uninstall the app off my phone and press run on android studio the app launches and logs in to a really old account that no longer exists on the DB; and I can't work out why this is happening.
My only thought is there is a userID stored on the device that isn't being removed, but that could be completely wrong. Any ideas?
I've added some log tags throughout the code and before the app is uninstalled the userID is correct and when it's been reinstalled it's the old one.
Shared Preferences are always cleared along with uninstalling app.
But since android-21 Backup task stores preferences by default to cloud. Later when you uninstall then install newer version .You are probably going to use restored preferences. To avoid that, just add this to your manifest ( or at least manifest for debug). -
<application ...
android:allowBackup="false">
...
</application>
Read this:http://developer.android.com/guide/topics/data/backup.html
It's Important to mention here that process of backup is blackbox .. you don't know when it starts, and period between checks ... so better for developing to disable it.
Related
I was using Firebase Auth Gmail Authentication. While doing some random tests on the app, I deleted the account from Firebase Auth Console while I was still logged in.
Now even if I uninstall the App and reinstall it, I still get the UID even though that UID is not present in the Firebase Auth Console. The first lines of code as soon as I open the app are as follows:
val uidz = FirebaseAuth.getInstance().currentUser?.uid.toString()
Log.d("uid as soon as i open the app",uidz)
Logcat Reads as
2022-09-27 21:40:01.802 23060-23060/com.example.alliaiseV1 D/uid as soon as i open the app: uZOs2XhBOrX4YU8qKem3lD4C7cy1
Strange parts:
This UID if I navigate to some other page and re-check the current user changes to null
Every time I uninstall and reinstall the App I see the same UID every time
It only happens on my device for every other device when I re-install the App the current UID returns as null but for my device, it returns as the above in logcat
Deleting App Data and Cache works but again if I reinstall I see the UID
I edited the code to :
val uidz = FirebaseAuth.getInstance().currentUser?.uid.toString()
Log.d("uid as soon as i open the app",uidz)
FirebaseAuth.getInstance().signOut()
Log.d("uidlogout", "${FirebaseAuth.getInstance().currentUser?.uid.toString()}")
The logcat I read for it is as follows :
2022-09-27 21:45:06.228 23964-23964/com.example.alliaiseV1 D/uid as soon as I open the app: uZOs2XhBOrX4YU8qKem3lD4C7cy1
2022-09-27 21:45:06.229 23964-23964/com.example.alliaiseV1 D/uidlogout: null
From where is this UID coming?
Why do I see the same UID every time?
How do I get rid of it?
While doing some random tests on the app, I deleted the account from Firebase Auth Console while I was still logged in. Now even if I uninstall the App and reinstall it, I still get the UID even though that UID is not present in the Firebase Auth Console.
If you accidentally deleted a user account from the Firebase console it doesn't mean that the current token automatically expires for that account. No, the current tokens will remain valid until it expires. If you however are interested in changing the expiration interval, please note that this option is available in your Firebase console.
If you want to force a user to sign out, then you can use FirebaseAuth#signOut() method, which:
Signs out the current user and clears it from the disk cache.
To prevent that from happening, you should consider creating authorization rules. In this way, you'll be able to differentiate users with valid tokens from the ones that have deleted accounts. If you are using, for example, the Realtime Database to keep a user's data, then you can check whether that user still exists in your security rules using the following line of code:
root.child('users').child(auth.uid).exists()
For more info, I recommend you read David East's answer in the following post:
Deletion of User in firebase does not trigger onAuth method
Sorry for the ambiguity in the question but it is actually quite a simple one.
When my android Application boots up I initialize AppCenter as follows:
AppCenter.start(
this, BuildConfig.APP_CENTER_SECRET,
Analytics::class.java, Crashes::class.java, Distribute::class.java
)
if(BuildConfig.FLAVOR != ApplicationVariants.ProductFlavors.PRODUCTION){
AppCenter.setLogLevel(Log.VERBOSE)
}
AppCenter.setUserId(UUID.randomUUID().toString())
Distribute.setUpdateTrack(UpdateTrack.PUBLIC)
Distribute.checkForUpdate()
However, when the user logs into the application I would like to set the UserId to the users email as follows once the user logs in:
JwtUtils.getIdentityTokenModel(requireContext())?.let {
AppCenter.setUserId(it.email)
}
Lastly when the user logs out I reset the user Id to a random guid. The reason for this is visibility on which user has which crash logs. This is a requirement from business.
However, in the app center crash logs, it seems the UserId never changes to the email even if an error occurs while the user is logged in.
My question is simple. Is there a restriction on how many times I am allowed to change the AppCenter User Id? I cannot seem to find it anywhere in the docs.
Thanks in advance
Please see these docs about userId API:
The value for the user ID is limited to 256 characters. It will be
shown with your crash reports but not used for aggregation or counts
of affected users. In case you set user ID multiple times, only the
last user ID will be used. You need to set the user ID yourself before
each application launch, because this value isn't stored by the SDK
between launches.
I've recently discovered that Firebase Auth saves itself on the device even after my app is uninstalled. I can't figure out how to REMOVE this old Auth info.
I don't want a user to still be signed in after uninstalling and reinstalling the app. If for no other reason than my own testing of what I expect to be "clean installs" on the same device.
I understand there is no easy way to capture an uninstall event, so I want to clear out any potential old Auth info on the first launch.
So I added code (which seems to work fine) to check if this is the first launch:
Boolean firstRun = prefs.getBoolean("firstrun", true);
if (firstRun) {
// delete everything an old user could have left behind
// ==> This is where I need help <==
prefs.edit().putBoolean("firstrun", false).apply();
} else {
// move along, not the first launch
}
I've tried (unsuccessfully):
FirebaseAuth authData = FirebaseAuth.getInstance();
authData.signOut();
These calls also seem to be the advice in this related question for iOS, but I haven't been able to apply its wisdom:
Firebase - Deleting and reinstalling app does not un-authenticate a user
Even after calling signOut() the app keeps logging me in under the old account!
My "logout" button uses FirebaseAuth.getInstance().signOut(); and works. Is there something odd (possessed?) about this "old" Auth instance that is being saved after an uninstall that it just won't die?
Specifically when I uninstall and then install/run from Android Studio:
at first authData and currentUser both are not null
I call the above code, trying to get rid of this old user
3 millisecond later (immediately after I call that
code) they are still NOT NULL.
Another 2 milliseconds, currentUser IS NULL (yay?)
Another 71 milliseconds... still null (so far so good)
Just under a second later... I'M SIGNED IN AS THE OLD USER?! How is this possible?
In the Firebase Console under Authentication, this account is shown as last signed in 6 days ago. So it's not somehow getting re-signed-in.
Does anyone know how to remove FirebaseAuth data from a device? I don't want to "delete" the user account, just remove all traces of it from this device.
Oddly enough, the account I keep getting unwillfully logged in under isn't even the last account that logged into my app on this device. And this was never a problem in the past (hence my not even knowing that Firebase saved Auth after uninstall). So it looks like Auth info isn't always saved after uninstall... but when it happens it's impossible to remove?
Any help much appreciated!
Add android:allowBackup="false" in your <application> in manifest:
From the docs:
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.
Try also FirebaseAuth.getInstance().getCurrentUser().delete
Firebase stores auth info in shared preference with file names starting with "com.google.firebase.auth.api.". Therefor if you delete these files as part of your log off flow it would help the purpose.
public void clearFirebaseAuthInfo(Context ctx)
{
File dir = new File(ctx.getFilesDir().getParent() + "/shared_prefs/");
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
if(children[i].contains("com.google.firebase.auth.api."))
{
new File(dir, children[i]).delete();
}
}
}
I have a SharedPreference that counts the amount of launches of my App. I expect this to be 0, when I install my app. Nevertheless it is 14. The same strange behaviour I observe with my database, that already exists on a fresh install.
I didn't intent to recycle my app data (like in the Google Cloud). On my device in the account settings the app-data sync is on. If I turn it off, and make a reinstall I observe the same behaviour.
Anyone has every observed such a behaviour? Is there any way to prevent recycling old data and force a proper install?
In Android Marshmallow Google introduced the "Auto Backup" feature which is turned on by default if your targetSdkVersion is >=23.
This will back up your database and SharedPreferences by default and restore it when you re-install the application. To turn this feature off you have to add android:allowBackup="false" to your AndroidManifest.xml.
More info here: Auto Backup for Apps
Review your code in AndroidManifest on tag application if it has android:allowBackup="false". If you don't have (by default it's true), your app participates on android backup and restore infrastructure and can happen exactly what you say.
More information in this post: What is "android:allowBackup"?
This needs to be handled from app side regarding shared Prefs.
Created a shared preference helper class and in the helper class have the below condition.
private static String SHARED_PREFS_VERSION = "SharedPrefsVersion"; // In this save the version of current shared Prefs.
void SharedPrefsHelper() {
if( BuildConfig.Version > getSharedPrefsVersion() ) {
SharedPreferences.Editor editor = prefs.edit(); editor.clear(); // Clear all the shared Prefs and update the current version.
SetSharedPrefsVersion(BuildConfig.Version);
}
}
For further reference look at :
https://stackoverflow.com/a/12381061/7364024
Is it possible to receive an event, if the user execute the "Clear Data" on the global app screen?
I create an account in my Application and save the name of the account as preference:
AccountManager accountManager = AccountManager.get(this.context);
Account account = new Account("my account name", "com.mchammer.cantouchthis");
accountManager.addAccountExplicitly(account, "", null);
now, if the user clears the data, the account on the system (used for synchronisation) have to removed too...
thanks for your help
You can't. Your app gets FCed on Clear Data, so you can't run any code.
BUT. You can use SharedPreferences to store a boolean pref, say isAccountInitialized.
You set it to true and store after your AccountManager logic has done creating the account. Then, every time you use the account for sync (or every time you launch your main activity, or your service is up do do something - this you'll have to figure out based on your app specifics) you pull the isAccountInitialized from shared prefs and if it's false and your account is present in AccountManager - that's your sign that user has executed Clear Data and it's time to remove the account from AccountManager. If it's false and there's no account - that's first launch.
// OFFTOP Nice package name :)
It is not possible as As soon as you press clear data. Your application process will be stopped. You can check this link for answer.