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 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
- Add Firebase to your Android project.
- Add the dependency for Firebase Authentication to your app-level
build.gradle
file:compile 'com.google.firebase:firebase-auth:9.6.1'
- 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
onCreate
method, get the shared instance of theFirebaseAuth
object:private FirebaseAuth mAuth; // ... mAuth = FirebaseAuth.getInstance();
- Set up an
AuthStateListener
that responds to changes in the user's sign-in state:private FirebaseAuth.AuthStateListener mAuthListener; // ... @Override protected void onCreate(Bundle savedInstanceState) { // ... mAuthListener = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if (user != null) { // User is signed in Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); } else { // User is signed out Log.d(TAG, "onAuthStateChanged:signed_out"); } // ... } }; // ... } @Override public void onStart() { super.onStart(); mAuth.addAuthStateListener(mAuthListener); } @Override public void onStop() { super.onStop(); if (mAuthListener != null) { mAuth.removeAuthStateListener(mAuthListener); } }
- Finally, call
signInAnonymously
to sign in as an anonymous user:mAuth.signInAnonymously() .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Log.d(TAG, "signInAnonymously:onComplete:" + task.isSuccessful()); // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { Log.w(TAG, "signInAnonymously", task.getException()); Toast.makeText(AnonymousAuthActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } // ... } });
If sign-in succeeds, theAuthStateListener
runs theonAuthStateChanged
callback, in which you can use thegetCurrentUser
method 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.signInWith
methods. For example, get the user's Google ID token, Facebook access token, or email address and password. Get an
AuthCredential
for the new authentication provider:Google Sign-In
AuthCredential credential = GoogleAuthProvider.getCredential(googleIdToken, null);
Facebook Login
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
Email-password sign-in
AuthCredential credential = EmailAuthProvider.getEmailAuthCredential(email, password);
Pass the
AuthCredential
object to the sign-in user'slinkWithCredential
method:mAuth.getCurrentUser().linkWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Log.d(TAG, "linkWithCredential:onComplete:" + task.isSuccessful()); // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { Toast.makeText(AnonymousAuthActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } // ... } });
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.