Stay organized with collectionsSave and categorize content based on your preferences.
You can useFirebase Authenticationto create and use temporary anonymous accounts
to authenticate with Firebase. These temporary anonymous accounts can be used to
allow users who haven't yet signed up to your app to work with data protected
by security rules. If an anonymous user decides to sign up to your app, you canlink their sign-in credentials to the anonymous
accountso that they can continue to work with their protected data in
future sessions.
In yourmodule (app-level) Gradle file(usually<project>/<app-module>/build.gradle.ktsor<project>/<app-module>/build.gradle),
add the dependency for theFirebase Authenticationlibrary for Android. We recommend using theFirebase Android BoMto control library versioning.
dependencies{// Import theBoMfor the Firebase platformimplementation(platform("com.google.firebase:firebase-bom:34.2.0"))// Add the dependency for theFirebase Authenticationlibrary// When using theBoM, you don't specify versions in Firebase library dependenciesimplementation("com.google.firebase:firebase-auth")}
By using theFirebase Android BoM,
your app will always use compatible versions of Firebase Android libraries.
(Alternative)
Add Firebase library dependencies without using theBoM
If you choose not to use theFirebase BoM, you must specify each Firebase library version
in its dependency line.
Note that if you usemultipleFirebase libraries in your app, we strongly
recommend using theBoMto manage library versions, which ensures that all versions are
compatible.
dependencies{// Add the dependency for theFirebase Authenticationlibrary// When NOT using theBoM, you must specify versions in Firebase library dependenciesimplementation("com.google.firebase:firebase-auth:24.0.1")}
If you haven't yet connected your app to your Firebase project, do so from
theFirebaseconsole.
On theSign-in Methodspage, enable theAnonymoussign-in method.
Optional: If you've upgraded your project toFirebase Authenticationwith Identity Platform, you can enable automatic clean-up. When
you enable this setting, anonymous accounts older than 30 days will be automatically
deleted. In projects with automatic clean-up enabled, anonymous authentication will no
longer count toward usage limits or billing quotas. SeeAutomatic clean-up.
Authenticate with Firebase anonymously
When a signed-out user uses an app feature that requires authentication with
Firebase, sign in the user anonymously by completing the following steps:
In your activity'sonCreatemethod, get the shared instance
of theFirebaseAuthobject:
When initializing your Activity, check to see if the user is currently signed in:
Kotlin
publicoverridefunonStart(){super.onStart()// Check if user is signed in (non-null) and update UI accordingly.valcurrentUser=auth.currentUserupdateUI(currentUser)}
@OverridepublicvoidonStart(){super.onStart();// Check if user is signed in (non-null) and update UI accordingly.FirebaseUsercurrentUser=mAuth.getCurrentUser();updateUI(currentUser);}
Finally, callsignInAnonymouslyto sign in as an anonymous
user:
Kotlin
auth.signInAnonymously().addOnCompleteListener(this){task->if(task.isSuccessful){// Sign in success, update UI with the signed-in user's informationLog.d(TAG,"signInAnonymously:success")valuser=auth.currentUserupdateUI(user)}else{// If sign in fails, display a message to the user.Log.w(TAG,"signInAnonymously:failure",task.exception)Toast.makeText(baseContext,"Authentication failed.",Toast.LENGTH_SHORT,).show()updateUI(null)}}
mAuth.signInAnonymously().addOnCompleteListener(this,newOnCompleteListener<AuthResult>(){@OverridepublicvoidonComplete(@NonNullTask<AuthResult>task){if(task.isSuccessful()){// Sign in success, update UI with the signed-in user's informationLog.d(TAG,"signInAnonymously:success");FirebaseUseruser=mAuth.getCurrentUser();updateUI(user);}else{// If sign in fails, display a message to the user.Log.w(TAG,"signInAnonymously:failure",task.getException());Toast.makeText(AnonymousAuthActivity.this,"Authentication failed.",Toast.LENGTH_SHORT).show();updateUI(null);}}});
If sign-in succeeds you can use thegetCurrentUsermethod to get the user's account data.
Convert an anonymous account to a permanent account
When an anonymous user signs up to your app, you might want to allow them to
continue their work with their new account—for example, you might want to
make the items the user added to their shopping cart before they signed up
available in their new account's shopping cart. To do so, complete the following
steps:
When the user signs up, complete the sign-in flow for the user's
authentication provider up to, but not including, calling one of theFirebaseAuth.signInWithmethods. For example, get the user's Google ID token,
Facebook access token, or email address and password.
Get anAuthCredentialfor the new authentication provider:
If the call tolinkWithCredentialsucceeds, the user's new account can
access the anonymous account's Firebase data.
Automatic clean-up
If you've upgraded your project toFirebase Authenticationwith Identity Platform, you can
enable automatic clean-up in theFirebaseconsole. When you enable this feature you allow
Firebase to automatically delete anonymous accounts older than 30 days. In projects with automatic
clean-up enabled, anonymous authentication will not count toward usage limits or billing quotas.
Any anonymous accounts created after enabling automatic clean-up might be automatically
deleted any time after 30 days post-creation.
Existing anonymous accounts will be eligible for automatic deletion 30 days after
enabling automatic clean-up.
If you turn automatic clean-up off, any anonymous accounts scheduled to be deleted will remain
scheduled to be deleted.
If you "upgrade" an anonymous account by linking it to any sign-in method, the account will
not get automatically deleted.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,[]]