In Firebase Authentication on Android, what is an anonymous account, and how do you upgrade a guest to Google sign-in without losing their data?
answer
- a real user, just no credential
- signInAnonymously gives a uid
- link, do not sign in
- linkWithCredential keeps the uid
- ERROR_CREDENTIAL_ALREADY_IN_USE means merge
basics
~20 sAn anonymous account is a real Firebase user with a uid but no sign-in credential, created by signInAnonymously. To upgrade, pass the Google credential to the current user's linkWithCredential, which attaches it and keeps the same uid.
solid answer
~40 s`signInAnonymously()` creates a real user in the project: it has a `uid`, an ID token and a refresh token, so Security Rules and a backend treat the guest like any signed-in user, but no email, password or provider is attached. The SDK persists it across restarts, yet once the guest signs out or clears app data the device holds no credential to get back in. To upgrade, finish the Google flow up to its credential (`GoogleAuthProvider.getCredential(googleIdToken, null)`) and call `currentUser.linkWithCredential(credential)` instead of a `signInWith...` method: Google is added to the same account, the `uid` is unchanged and everything keyed by it carries over. If that Google account already belongs to another Firebase user, linking fails with `ERROR_CREDENTIAL_ALREADY_IN_USE`; you sign in with the credential and merge the guest's data yourself.
code
kotlin · 26 linesimport com.google.firebase.Firebase
import com.google.firebase.auth.FirebaseAuthException
import com.google.firebase.auth.GoogleAuthProvider
import com.google.firebase.auth.auth
fun upgradeGuest(googleIdToken: String, onDone: (uid: String?) -> Unit) {
val auth = Firebase.auth
val guest = auth.currentUser ?: return onDone(null)
val credential = GoogleAuthProvider.getCredential(googleIdToken, null)
guest.linkWithCredential(credential)
.addOnSuccessListener { result -> onDone(result.user?.uid) } // same uid as the guest
.addOnFailureListener { e ->
if (e is FirebaseAuthException && e.errorCode == "ERROR_CREDENTIAL_ALREADY_IN_USE") {
val guestUid = guest.uid
auth.signInWithCredential(credential)
.addOnSuccessListener { result ->
// Move the notes stored under guestUid to result.user's account here.
onDone(result.user?.uid)
}
.addOnFailureListener { onDone(null) }
} else {
onDone(null)
}
}
}go deeper
Know that signInAnonymously creates a real account with a uid, and that linkWithCredential, not a sign-in call, turns the guest into a permanent account.
Explain why the uid survives linking, what ERROR_CREDENTIAL_ALREADY_IN_USE means, and walk through signing in with the credential and merging the guest's data.
Cover the production edges: guests losing data on sign-out, sign-up throttling per IP, and checking firebase.sign_in_provider on the backend before granting anything meant for permanent accounts.
Weigh anonymous-first onboarding against forcing sign-up: data owned from the first tap, against orphaned guest accounts, a merge policy the team must own, and clean-up that needs the Identity Platform upgrade.
## What an anonymous account is Firebase Authentication can create a **temporary anonymous account** for someone who has not signed up yet. It is not a local placeholder: `signInAnonymously()` creates a user record in the project's user database with its own **`uid`**, and the device receives an **ID token** and a **refresh token** for it like any other sign-in. Everything that trusts a Firebase identity therefore works for the guest: - Security Rules see a non-null `request.auth` carrying the guest's `uid`. - A backend that verifies ID tokens gets a valid token whose `firebase.sign_in_provider` is `anonymous`. - Data the app stores under that `uid` (the notes in a notes app, say) belongs to the account. What the account lacks is a **credential**: no email and password, no provider such as Google. That is the whole difference, and it explains its main risk. ## Creating one on Android 1. In the Firebase console, enable the **Anonymous** provider on the Sign-in method tab; until then the call fails. 2. Add `com.google.firebase:firebase-auth` (through the Firebase BoM) and get the instance with `Firebase.auth`. 3. On start, read `auth.currentUser`; if it is null and the user opens a feature that needs an identity, call `auth.signInAnonymously()`. The SDK persists the signed-in user, so the same guest comes back after an app restart. But if the guest **signs out**, or the app's data is cleared, the device no longer holds anything that can sign back into that account: there is no password to type and no provider to ask. A later `signInAnonymously()` creates a *new* anonymous user with a new `uid`, and the old notes sit orphaned under the old one. ## Upgrading the guest: link, do not sign in When the guest decides to keep their notes, finish the Google flow only up to the point where you hold its result (on Android, a Google ID token) and turn it into a Firebase `AuthCredential` with `GoogleAuthProvider.getCredential(googleIdToken, null)`. How the app obtains that Google ID token is a Google Play services topic; here it is just an input. Then call `linkWithCredential(credential)` on the anonymous `currentUser`. Linking **adds a provider to the existing account**. The `uid` does not change, so every note, rule and backend row keyed by it carries over with no migration. From then on the user can sign in with Google on any device and land on the same account, which is no longer anonymous. | Call | Effect | The guest's `uid` afterwards | |---|---|---| | `currentUser.linkWithCredential(cred)` | adds Google to the guest's own account | kept; it is now the permanent account | | `auth.signInWithCredential(cred)` | switches to the Google account's own user, creating one if it is new | left behind, still anonymous | The common mistake is to call `signInWithCredential` because that is what the ordinary sign-in screen does. It appears to work, since the user is signed in with Google, but as a different `uid`, and the guest's data is stranded. ## When the credential is already in use Linking fails if that Google account is **already linked to another Firebase user**: the person used the app before, signed in with Google, and is now a guest on a new phone. The task fails with the error code `ERROR_CREDENTIAL_ALREADY_IN_USE`. Firebase does not merge the two accounts for you; the documented path is: 1. Keep a reference to the guest (`val guest = auth.currentUser`) and its `uid`. 2. Call `auth.signInWithCredential(credential)` to become the existing Google user. 3. Merge the guest's data into that account in your own code or backend, then decide what happens to the guest account. How to merge (which copy wins, what to do with duplicates) is the app's own decision; merging accounts across identity providers is a wider identity-design subject of its own. ## Limits and lifecycle worth knowing - **Sign-up throttling.** Firebase limits how many new email/password and anonymous accounts one IP address can create in a short period; the quota can be changed temporarily from the console. - **Automatic clean-up** is opt-in and needs a project upgraded to Firebase Authentication with Identity Platform: anonymous accounts older than 30 days then become eligible for deletion. An account you upgraded by linking is not deleted. - **Unlinking** a provider with `unlink(providerId)` stops sign-in with it; signing in with that provider again later creates a new, separate account instead of restoring the link.
- How can your backend tell that a verified Firebase ID token belongs to an anonymous guest?Read the decoded token's `firebase.sign_in_provider`: a guest session reports `anonymous`. The `firebase.identities` map lists the identities linked to the account (email, phone, `google.com` and so on), and a guest has none. Gate anything meant for permanent accounts on those fields from the verified token, never on a flag the app sends.
- A user unlinks Google from their Firebase account and later signs in with Google again. What happens?`unlink(providerId)` removes Google from the account, and Firebase does not remember the old link. Signing in with Google afterwards creates a new, separate Firebase user with a new `uid`, so the original account's data is not reachable from it. To reattach Google, link it again while signed in to the original account.
- Do anonymous Firebase accounts pile up in the project forever?By default they stay until something deletes them. A project upgraded to Firebase Authentication with Identity Platform can enable automatic clean-up, which makes anonymous accounts older than 30 days eligible for deletion; accounts upgraded by linking are kept. Firebase also throttles new anonymous sign-ups per IP address.
- An anonymous user exists only on the device, so the backend cannot see it.
- To upgrade a guest, call signInWithCredential with the Google credential.
- Linking gives the user a new uid, so the guest's data must be copied over.
- A guest who signs out can sign back in to the same anonymous account later.
- Firebase deletes anonymous accounts after 30 days in every project.