What happens to Firebase anonymous users?

FirebaseFirebase Authentication

Firebase Problem Overview


I want to know what will happen to the users of my app that I used anonymous sign in method for them.

The Firebase documentation is really BAD and didn't explain everything and expect developer to find out himself. I found in its old version documentation that anonymous session will expires based on the expiration time has been set in Login & Auth tab, but even there didn't mention this means just the session ends or it means that user id will remove also from my app users list or what EXACTLY happened?

I found this answer but it really is not acceptable. The number of anonymous users will grow very very fast if you do a web app and make every thing hard. I even cannot see the number of my app users in my dashboard!!!!! So, what should i do? should i develop a dashboard for my data myself or Firebase team should do it? At least for managing users i should have more power than just searching user with their email and when you use custom login you cannot do this also.

Firebase Solutions


Solution 1 - Firebase

Anonymous users don't expire, and there isn't currently any automated way to purge them.

Firebase doesn't automatically remove them because it doesn't really know if a user is still storing data linked to that login - only the app creator does. Imagine if you are playing a puzzle game on your phone, and get to level 100. Then when you go to play level 101 next year, all progress is lost. Firebase can't just assume a user being inactive for a year means that the account can be removed.

There is a couple tools that should help, though.

  1. Admin SDK & Firebase CLI list users.

  2. Linking multiple auth providers

  3. Auth State Persistence

Once you list your users, you can check that each doesn't have any other providers, and hasn't been used recently, doesn't have data stored, and delete them.

Better, though, would be to ensure that only one account is created per user. If you create an anonymous account to help users store data before logging in, you may want to consider prompting them to link a auth provider (like Google or email). If you link the account, rather than creating a new one, you'll avoid abandoned accounts from active users.

In general, you will also want to make sure to use auth state persistence to ensure that there aren't more accounts than necessary being created. Creating 1 account per new visitor, rather than 1 per time someone repeatedly visits your page, will significantly help keep user growth in check.

Solution 2 - Firebase

In my case, I am using the anonymous sign-in method for authentication without the knowledge of the user. Each time when the user leaves the app, delete the anonymous user by -

FirebaseAuth.getinstance().currentuser?.delete()

There will be no stacking up of anonymous user with this and limits the number of anonymous user in the app

Solution 3 - Firebase

There is a possible cloud function for that. Check: delete-unused-accounts-cron

This function deletes unused accounts after a certain time. Which might be also helpfull for nonanonymous users.

If you only want to delete anonymous users or check only for them (for example delete after a different inactive time than normal users) you can identify them by checking:

const inactiveUsers = result.users.filter(
        user => {
            isAnonymous = user.providerData.length == 0;
            //do something when anonymous
        });

Solution 4 - Firebase

If you'd like anonymous users to be removed from your user list, you'll have to write a service to do that for you.

Since firebase doesn't provide a way to list registered users, you'll have to make sure you're storing some sort of user list in the database. You can then use the node.js admin sdk to get user data, check if the user is anonymous, and find when the user was created. For performance reasons, you may wish to store this information in a special area of your database and retrieve it all at once. Once you've identified a stale anonymous user they can be easily deleted.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAliView Question on Stackoverflow
Solution 1 - FirebaseKianaView Answer on Stackoverflow
Solution 2 - FirebaseAnshul GargView Answer on Stackoverflow
Solution 3 - FirebasePaulView Answer on Stackoverflow
Solution 4 - FirebasenloewenView Answer on Stackoverflow