Our app is using Firebase to handle the login process, but we notice there are a lot of wasted anonymous accounts at the Firebase console.
Step 1. When the user first login, we will allow the user to enter the app anonymously, let’s say we get an Anonymous Account A;
Step 2. Then the user may choose to sign in with Google, then we link Anonymous Account A with the user’s Gmail and the Anonymous Account A changed to Permanent Account A;
Step 3. Then If the user logs out the Permanent Account A, we will restart the App and generate a new Anonymous Account B to enter the app.
Step 4. Then If the user logs in with Permanent Account A and then logs out again, We will restart the App and generate a new Anonymous Account C to enter the app.
And Anonymous Account B is totally wasted.
Our question is at Step 4, when the user log out Permanent Account A, can we just have a to reuse the Anonymous Account B to enter the app?
2
Answers
Firstly, it’s not really possible to do what you’re describing. Secondly, it’s not a good idea, because you can’t know for sure when someone is "permanently" signed out of any given account. All you can do is try to figure out how long an account has been unused. Also, you don’t want to risk re-using old user data for a new user.
The abandoned accounts are not really "wasted" at all. They cost you nothing and do not impact the performance of your application.
If you absolutely must remove old accounts, you should write some backend code using the Firebase Admin SDK to find and delete old accounts after they are unused for some amount of time.
Step 1. That’s a widely used practice in app development, to let your users try your app before becoming real users. In Firebase, this means linking (converting) an anonymous account into a permanent account, such as Google in your case.
Step 2. When you link an anonymous account with Google, the anonymous account will no longer exist, because it is converted. This also means that the UID that came from the Anonymous Authentication will be the same after the linking account operation is complete.
Step 3. If the user logs out of permanent (Account A), I don’t see any reason why you’ll have to restart the app and generate a new Anonymous (Account B) to enter the app. Since the permanent (Account A) was already converted, you can only use the app with the new privileges and sign out the user from Google + Firebase only when needed. So I recommend never creating a new Anonymous (Account B) account in such a case.
Step 4. The same as in Step 3.
There is no way you can reuse/recover an anonymous account once it is converted. Even if you unlink an auth provider, the
isAnonymous
flag will still remainfalse
. It will not becometrue
again after unlinking Google or any other provider. Please remember, that this is the whole point of linking an account, to attach a real identity, no matter what the provider is. This means that you allow the user to effectively "upgrade" their anonymous account into a fully privileged account. If you want an account to remain anonymous, then you should never link a provider to it.Lastly, as also @DougStevenson mentioned in his answer, having multiple anonymous accounts isn’t a "waste". Besides that, those anonymous accounts do not impact the performance of your application in any way. If you want to delete old accounts, then I recommend you create a Cloud Function and delete accounts older than x days/months/years.