You can use Firebase Authentication to 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 can link their sign-in credentials to the anonymous account so that they can continue to work with their protected data in future sessions.
Before you begin
- If you haven't already, add Firebase to your Android project.
- In your project-level
build.gradlefile, make sure to include Google's Maven repository in both yourbuildscriptandallprojectssections. - Add the dependency for the Firebase Authentication Android library to your
module (app-level) Gradle file (usually
app/build.gradle):implementation 'com.google.firebase:firebase-auth:19.0.0'
- If you haven't yet connected your app to your Firebase project, do so from the Firebase console.
- Enable anonymous auth:
- In the Firebase console, open the Auth section.
- On the Sign-in Methods page, enable the Anonymous sign-in method.
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's
onCreatemethod, get the shared instance of theFirebaseAuthobject:Java
private FirebaseAuth mAuth; // ... // Initialize Firebase Auth mAuth = FirebaseAuth.getInstance();
Kotlin
private lateinit var auth: FirebaseAuth // ... // Initialize Firebase Auth auth = FirebaseAuth.getInstance()
- When initializing your Activity, check to see if the user is currently signed in:
Java
@Override public void onStart() { super.onStart(); // Check if user is signed in (non-null) and update UI accordingly. FirebaseUser currentUser = mAuth.getCurrentUser(); updateUI(currentUser); }Kotlin
public override fun onStart() { super.onStart() // Check if user is signed in (non-null) and update UI accordingly. val currentUser = auth.currentUser updateUI(currentUser) } - Finally, call
signInAnonymouslyto sign in as an anonymous user:If sign-in succeeds you can use theJava
mAuth.signInAnonymously() .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInAnonymously:success"); FirebaseUser user = 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); } // ... } });Kotlin
auth.signInAnonymously() .addOnCompleteListener(this) { task -> if (task.isSuccessful) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInAnonymously:success") val user = auth.currentUser updateUI(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) } // ... }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 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 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
Java
AuthCredential credential = GoogleAuthProvider.getCredential(googleIdToken, null);
Kotlin
val credential = GoogleAuthProvider.getCredential(googleIdToken, null)
Facebook Login
Java
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
Kotlin
val credential = FacebookAuthProvider.getCredential(token.token)
Email-password sign-in
Java
AuthCredential credential = EmailAuthProvider.getCredential(email, password);
Kotlin
val credential = EmailAuthProvider.getCredential(email, password)
Pass the
AuthCredentialobject to the sign-in user'slinkWithCredentialmethod:Java
mAuth.getCurrentUser().linkWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { Log.d(TAG, "linkWithCredential:success"); FirebaseUser user = 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); } // ... } });Kotlin
auth.currentUser?.linkWithCredential(credential) ?.addOnCompleteListener(this) { task -> if (task.isSuccessful) { Log.d(TAG, "linkWithCredential:success") val user = task.result?.user updateUI(user) } else { Log.w(TAG, "linkWithCredential:failure", task.exception) Toast.makeText(baseContext, "Authentication failed.", Toast.LENGTH_SHORT).show() updateUI(null) } // ... }
If the call to linkWithCredential succeeds, the user's new account can
access the anonymous account's Firebase data.
Next steps
Now that users can authenticate with Firebase, you can control their access to data in your Firebase database using Firebase rules.

