Authenticate with Firebase Anonymously on Android Stay organized with collections Save and categorize content based on your preferences.
You can useFirebase Authentication to create and use temporary anonymous accountsto authenticate with Firebase. These temporary anonymous accounts can be used toallow users who haven't yet signed up to your app to work with data protectedby security rules. If an anonymous user decides to sign up to your app, you canlink their sign-in credentials to the anonymousaccount so that they can continue to work with their protected data infuture sessions.
Before you begin
- If you haven't already,add Firebase to your Android project.
- In yourmodule (app-level) Gradle file(usually
<project>/<app-module>/build.gradle.ktsor<project>/<app-module>/build.gradle),add the dependency for theFirebase Authentication library for Android. We recommend using theFirebase Android BoMto control library versioning.dependencies{// Import theBoM for the Firebase platformimplementation(platform("com.google.firebase:firebase-bom:34.9.0"))// Add the dependency for theFirebase Authentication library// 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 usemultiple Firebase libraries in your app, we strongly recommend using theBoM to manage library versions, which ensures that all versions are compatible.
dependencies{// Add the dependency for theFirebase Authentication library// 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 theFirebase console.
- Enable anonymous auth:
- In theFirebase console, open theAuth section.
- On theSign-in Methods page, enable theAnonymous sign-in method.
- Optional: If you've upgraded your project toFirebase Authentication with 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 withFirebase, sign in the user anonymously by completing the following steps:
- In your activity's
onCreatemethod, get the shared instance of theFirebaseAuthobject:Kotlin
privatelateinitvarauth:FirebaseAuth// ...// Initialize Firebase Authauth=Firebase.auth
Java
privateFirebaseAuthmAuth;// ...// Initialize Firebase AuthmAuth=FirebaseAuth.getInstance();
- 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)}
Java
@OverridepublicvoidonStart(){super.onStart();// Check if user is signed in (non-null) and update UI accordingly.FirebaseUsercurrentUser=mAuth.getCurrentUser();updateUI(currentUser);}
- Finally, call
signInAnonymouslyto sign in as an anonymous user:If sign-in succeeds you can use theKotlin
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)}}
Java
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);}}});
getCurrentUsermethod 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 tocontinue their work with their new account—for example, you might want tomake the items the user added to their shopping cart before they signed upavailable in their new account's shopping cart. To do so, complete the followingsteps:
- When the user signs up, complete the sign-in flow for the user's authentication provider up to, but not including, calling one of the
FirebaseAuth.signInWithmethods. For example, get the user's Google ID token, Facebook access token, or email address and password. Get an
AuthCredentialfor the new authentication provider:Google Sign-In
Kotlin
valcredential=GoogleAuthProvider.getCredential(googleIdToken,null)
Java
AuthCredentialcredential=GoogleAuthProvider.getCredential(googleIdToken,null);
Facebook Login
Kotlin
valcredential=FacebookAuthProvider.getCredential(token.token)
Java
AuthCredentialcredential=FacebookAuthProvider.getCredential(token.getToken());
Email-password sign-in
Kotlin
valcredential=EmailAuthProvider.getCredential(email,password)
Java
AuthCredentialcredential=EmailAuthProvider.getCredential(email,password);
Pass the
AuthCredentialobject to the sign-in user'slinkWithCredentialmethod:Kotlin
auth.currentUser!!.linkWithCredential(credential).addOnCompleteListener(this){task->if(task.isSuccessful){Log.d(TAG,"linkWithCredential:success")valuser=task.result?.userupdateUI(user)}else{Log.w(TAG,"linkWithCredential:failure",task.exception)Toast.makeText(baseContext,"Authentication failed.",Toast.LENGTH_SHORT,).show()updateUI(null)}}
Java
mAuth.getCurrentUser().linkWithCredential(credential).addOnCompleteListener(this,newOnCompleteListener<AuthResult>(){@OverridepublicvoidonComplete(@NonNullTask<AuthResult>task){if(task.isSuccessful()){Log.d(TAG,"linkWithCredential:success");FirebaseUseruser=task.getResult().getUser();updateUI(user);}else{Log.w(TAG,"linkWithCredential:failure",task.getException());Toast.makeText(AnonymousAuthActivity.this,"Authentication failed.",Toast.LENGTH_SHORT).show();updateUI(null);}}});
If the call tolinkWithCredential succeeds, the user's new account canaccess the anonymous account's Firebase data.
Automatic clean-up
If you've upgraded your project toFirebase Authentication with Identity Platform, you can enable automatic clean-up in theFirebase console. 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.
If you want to see how many users will be affected before you enable this feature, and you'veupgraded your project toFirebase Authentication with Identity Platform, you can filter byis_anon inCloudLogging.
Next steps
Now that users can authenticate with Firebase, you can control their access todata in your Firebase database usingFirebase rules.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-18 UTC.