Authenticate with Firebase Anonymously

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

  1. If you haven't already, follow the steps in the Get started guide.

  2. Enable Anonymous sign-in:

    • In the Firebase console's Authentication section, open the Sign in method page.
    • From the Sign in method page, enable the Anonymous sign-in method and click Save.

Authenticate with Firebase anonymously

When a signed-out user uses an app feature that requires authentication with Firebase, sign in the user anonymously by calling signInAnonymously():

try {
  final userCredential =
      await FirebaseAuth.instance.signInAnonymously();
  print("Signed in with temporary account.");
} on FirebaseAuthException catch (e) {
  switch (e.code) {
    case "operation-not-allowed":
      print("Anonymous auth hasn't been enabled for this project.");
      break;
    default:
      print("Unknown error.");
  }
}

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:

  1. 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 signInWith- methods. For example, get the user's Google ID token, Facebook access token, or email address and password.

  2. Get a Credential object for the new authentication provider:

    // Google Sign-in
    final credential = GoogleAuthProvider.credential(idToken: idToken);
    
    // Email and password sign-in
    final credential =
        EmailAuthProvider.credential(email: emailAddress, password: password);
    
    // Etc.
    
  3. Pass the Credential object to the sign-in user's linkWithCredential() method:

    try {
      final userCredential = await FirebaseAuth.instance.currentUser
          ?.linkWithCredential(credential);
    } on FirebaseAuthException catch (e) {
      switch (e.code) {
        case "provider-already-linked":
          print("The provider has already been linked to the user.");
          break;
        case "invalid-credential":
          print("The provider's credential is not valid.");
          break;
        case "credential-already-in-use":
          print("The account corresponding to the credential already exists, "
              "or is already linked to a Firebase User.");
          break;
        // See the API reference for the full list of error codes.
        default:
          print("Unknown error.");
      }
      ```
    

If the call to linkWithCredential() succeeds, the user's new account can access the anonymous account's Firebase data.

Next steps

After a user creates a new account, this account is stored as part of your Firebase project, and can be used to identify a user across every app in your project, regardless of what sign-in method the user used.

In your apps, you can get the user's basic profile information from the User object. See Manage Users.

In your Firebase Realtime Database and Cloud Storage Security Rules, you can get the signed-in user's unique user ID from the auth variable, and use it to control what data a user can access.

You can allow users to sign in to your app using multiple authentication providers by linking auth provider credentials) to an existing user account.

To sign out a user, call signOut():

await FirebaseAuth.instance.signOut();