I want to make a new user can see some contents in my app before they decide to log in using Gmail, Facebook, or email and password. so first I need to make them sign in anonymously when they install the app using this code
auth.signInAnonymously()
And then, after they play around, they decide to sign in using Google account, and I convert that user with specific UID (let say the UID is q1q2q3) to use Google as authentication provider using this code.
auth.currentUser!!.linkWithCredential(credential)
and then let say, they uninstall, and reinstall the app. so they will sign in anonymously again and they will get different UID as an anonymous user (let say the new UID is aXXXX12345b ), even though if they log in using Google again, they will sign in as q1q2q3
so now I have a redundant anonymous user (aXXXX12345b). is it okay to have a redundant anonymous authenticated users like this? I mean, I am worried that I will hit some limit from firebase authentication. what should I do ? is there a better approach for cases like this? I don't know if this is a common practice or not
and then let say, they uninstall, and reinstall the app. so they will sign in anonymously again and they will get different uid as anonymous user (let say the new uid is aXXXX12345b )
Yes that's correct, a new UID is generated for the new anonymous user.
even though if they login using google again, they will sign in as q1q2q3
At this moment in your database you have two users. The first one was an anonymous user that was converted into a real user using a Google account, and the second one is an anonymous user that is not converted yet. However, if you want to convert the second user into a real user using the exact same account as before, this is not possible because that account already exists and therefore an Exception with following message will be thrown:
This credential is already associated with a different user account.
You can convert the second anonymous user only if you are using a different Google account.
so now I have a redundant anonymous user (aXXXX12345b)
No, you don't. You have a real account and an anonymous account. The second can be redundant only if the user decides to uninstall the app. If you are worried about the Firebase limitation, offer to the anonymous user the possibility to logout. When the user clicks on the logout button, there are two things that you need to do:
If you store data for the anonymous user in the database, Cloud Firestore or Firebase Realtime Database, as well in the Firebase Storage, delete it.
db.collection("users").document(uid).delete().addOnCompleteListener(/* ... /*);
Delete the user from the Firebase authentication too using:
firebaseUser.delete().addOnCompleteListener(/* ... /*);
In this way there will be nothing left on Firebase servers.
is it okay to have a redundant anonymous authenticated users like this?
No, it's not ok. That's why you should clean the data as much as possible.