Authenticate with Firebase Anonymously
Notice
This page is archived and might not reflect the latest version of the FlutterFire plugins. You can find the latest information on firebase.google.com:
https://firebase.google.com/docs/auth/flutter/anonymous-auth
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 beginIf you haven't already, follow the steps in the Get started guide.
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 anonymouslyWhen a signed-out user uses an app feature that requires authentication with
Firebase, sign in the user anonymously by calling signInAnonymously()
:
note
To protect your project from abuse, Firebase limits the number of new email/password and anonymous sign-ups that your application can have from the same IP address in a short period of time. You can request and schedule temporary changes to this quota from the Firebase console.
#
Convert an anonymous account to a permanent accountWhen 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
signInWith
- methods. For example, get the user's Google ID token, Facebook access token, or email address and password.Get a
Credential
object for the new authentication provider:Pass the
Credential
object to the sign-in user'slinkWithCredential()
method:
If the call to linkWithCredential()
succeeds, the user's new account can
access the anonymous account's Firebase data.
note
This technique can also be used to link any two accounts.
#
Next stepsAfter 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()
: