Get Started with Firebase Authentication in Unity

You can use Firebase Authentication to allow users to sign in to your game using one or more sign-in methods, including email address and password sign-in, and federated identity providers such as Google Sign-in and Facebook Login. This tutorial gets you started with Firebase Authentication by showing you how to add email address and password sign-in to your game.

Connect your app to Firebase

Install the Firebase SDK.

Sign up new users

Create a form that allows new users to register with your game using their email address and a password. When a user completes the form, validate the email address and password provided by the user, then pass them to the CreateUserWithEmailAndPasswordAsync method:

auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task => {
  if (task.IsCanceled) {
    Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
    return;
  }
  if (task.IsFaulted) {
    Debug.LogError("CreateUserWithEmailAndPasswordAsync encountered an error: " + task.Exception);
    return;
  }

  // Firebase user has been created.
  Firebase.Auth.FirebaseUser newUser = task.Result;
  Debug.LogFormat("Firebase user created successfully: {0} ({1})",
      newUser.DisplayName, newUser.UserId);
});

Sign in existing users

Create a form that allows existing users to sign in using their email address and password. When a user completes the form, call the SignInWithEmailAndPasswordAsync method:

auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task => {
  if (task.IsCanceled) {
    Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
    return;
  }
  if (task.IsFaulted) {
    Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
    return;
  }

  Firebase.Auth.FirebaseUser newUser = task.Result;
  Debug.LogFormat("User signed in successfully: {0} ({1})",
      newUser.DisplayName, newUser.UserId);
});

Set an authentication state change event handler and get user data

To respond to sign-in and sign-out events, attach an event handler to the global authentication object. This handler gets called whenever the user's sign-in state changes. Because the handler runs only after the authentication object is fully initialized and after any network calls have completed, it is the best place to get information about the signed-in user.

Register the event handler using the FirebaseAuth object's StateChanged field. When a user successfully signs in, you can get information about the user in the event handler.

void InitializeFirebase() {
  auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
  auth.StateChanged += AuthStateChanged;
  AuthStateChanged(this, null);
}

void AuthStateChanged(object sender, System.EventArgs eventArgs) {
  if (auth.CurrentUser != user) {
    bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null;
    if (!signedIn && user != null) {
      DebugLog("Signed out " + user.UserId);
    }
    user = auth.CurrentUser;
    if (signedIn) {
      DebugLog("Signed in " + user.UserId);
      displayName = user.DisplayName ?? "";
      emailAddress = user.Email ?? "";
      photoUrl = user.PhotoUrl ?? "";
    }
  }
}

Next steps

Learn how to add support for other identity providers and anonymous guest accounts:

Send feedback about...

Need help? Visit our support page.