|
| 1 | +--- |
| 2 | +title: Authenticate with Firebase Anonymously |
| 3 | +sidebar_label: Anonymous Auth |
| 4 | +--- |
| 5 | + |
| 6 | +You can use Firebase Authentication to create and use temporary anonymous accounts |
| 7 | +to authenticate with Firebase. These temporary anonymous accounts can be used to |
| 8 | +allow users who haven't yet signed up to your app to work with data protected |
| 9 | +by security rules. If an anonymous user decides to sign up to your app, you can |
| 10 | +[link their sign-in credentials](account-linking) to the anonymous account so |
| 11 | +that they can continue to work with their protected data in future sessions. |
| 12 | + |
| 13 | +## Before you begin |
| 14 | + |
| 15 | +1. If you haven't already, follow the steps in the [Get started](start) guide. |
| 16 | + |
| 17 | +1. Enable Anonymous sign-in: |
| 18 | + |
| 19 | + - In the Firebase console's **Authentication** section, open the |
| 20 | + [Sign in method](https://console.firebase.google.com/project/_/authentication/providers) |
| 21 | + page. |
| 22 | + - From the **Sign in method** page, enable the **Anonymous sign-in** |
| 23 | + method and click **Save**. |
| 24 | + |
| 25 | +## Authenticate with Firebase anonymously |
| 26 | + |
| 27 | +When a signed-out user uses an app feature that requires authentication with |
| 28 | +Firebase, sign in the user anonymously by calling `signInAnonymously()`: |
| 29 | + |
| 30 | +```dart |
| 31 | +try { |
| 32 | + final userCredential = |
| 33 | + await FirebaseAuth.instance.signInAnonymously(); |
| 34 | + print("Signed in with temporary account."); |
| 35 | +} on FirebaseAuthException catch (e) { |
| 36 | + switch (e.code) { |
| 37 | + case "operation-not-allowed": |
| 38 | + print("Anonymous auth hasn't been enabled for this project."); |
| 39 | + break; |
| 40 | + default: |
| 41 | + print("Unkown error."); |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +:::note |
| 47 | +To protect your project from abuse, Firebase limits the number of new |
| 48 | +email/password and anonymous sign-ups that your application can have from the |
| 49 | +same IP address in a short period of time. You can request and schedule |
| 50 | +temporary changes to this quota from the |
| 51 | +[Firebase console](https://console.firebase.google.com/project/_/authentication/providers). |
| 52 | +::: |
| 53 | + |
| 54 | +## Convert an anonymous account to a permanent account |
| 55 | + |
| 56 | +When an anonymous user signs up to your app, you might want to allow them to |
| 57 | +continue their work with their new account—for example, you might want to |
| 58 | +make the items the user added to their shopping cart before they signed up |
| 59 | +available in their new account's shopping cart. To do so, complete the following |
| 60 | +steps: |
| 61 | + |
| 62 | +1. When the user signs up, complete the sign-in flow for the user's |
| 63 | + authentication provider up to, but not including, calling one of the |
| 64 | + `signInWith`- methods. For example, get the user's Google ID token, |
| 65 | + Facebook access token, or email address and password. |
| 66 | + |
| 67 | +1. Get a `Credential` object for the new authentication provider: |
| 68 | + |
| 69 | + ```dart |
| 70 | + // Google Sign-in |
| 71 | + final credential = GoogleAuthProvider.credential(idToken: idToken); |
| 72 | +
|
| 73 | + // Email and password sign-in |
| 74 | + final credential = |
| 75 | + EmailAuthProvider.credential(email: emailAddress, password: password); |
| 76 | + |
| 77 | + // Etc. |
| 78 | + ``` |
| 79 | + |
| 80 | +1. Pass the `Credential` object to the sign-in user's `linkWithCredential()` |
| 81 | + method: |
| 82 | + |
| 83 | + ```dart |
| 84 | + try { |
| 85 | + final userCredential = await FirebaseAuth.instance.currentUser |
| 86 | + ?.linkWithCredential(credential); |
| 87 | + } on FirebaseAuthException catch (e) { |
| 88 | + switch (e.code) { |
| 89 | + case "provider-already-linked": |
| 90 | + print("The provider has already been linked to the user."); |
| 91 | + break; |
| 92 | + case "invalid-credential": |
| 93 | + print("The provider's credential is not valid."); |
| 94 | + break; |
| 95 | + case "credential-already-in-use": |
| 96 | + print("The account corresponding to the credential already exists, " |
| 97 | + "or is already linked to a Firebase User."); |
| 98 | + break; |
| 99 | + // See the API reference for the full list of error codes. |
| 100 | + default: |
| 101 | + print("Unknown error."); |
| 102 | + } |
| 103 | + ``` |
| 104 | + |
| 105 | +If the call to `linkWithCredential()` succeeds, the user's new account can |
| 106 | +access the anonymous account's Firebase data. |
| 107 | + |
| 108 | +:::note |
| 109 | +This technique can also be used to [link any two accounts](account-linking). |
| 110 | +::: |
| 111 | + |
| 112 | + |
| 113 | +## Next steps |
| 114 | + |
| 115 | +After a user creates a new account, this account is stored as part of your |
| 116 | +Firebase project, and can be used to identify a user across every app in your |
| 117 | +project, regardless of what sign-in method the user used. |
| 118 | + |
| 119 | +In your apps, you can get the user's basic profile information from the |
| 120 | +`User` object. See [Manage Users](manage-users). |
| 121 | + |
| 122 | +In your Firebase Realtime Database and Cloud Storage Security Rules, you can |
| 123 | +get the signed-in user's unique user ID from the `auth` variable, and use it to |
| 124 | +control what data a user can access. |
| 125 | + |
| 126 | +You can allow users to sign in to your app using multiple authentication |
| 127 | +providers by [linking auth provider credentials](account-linking)) to an |
| 128 | +existing user account. |
| 129 | + |
| 130 | +To sign out a user, call `signOut()`: |
| 131 | + |
| 132 | +```dart |
| 133 | +await FirebaseAuth.instance.signOut(); |
| 134 | +``` |
0 commit comments