Authenticate Using GitHub and Unity (original) (raw)

You can let your users authenticate with Firebase using their GitHub accounts by integrating GitHub authentication into your app.

Before you begin

Before you can useFirebase Authentication, you need to:

Note that adding Firebase to your Unity project involves tasks both in theFirebase console and in your open Unity project (for example, you download Firebase config files from the console, then move them into your Unity project).

Access the Firebase.Auth class

The FirebaseAuth class is the gateway for all API calls. It is accessible through FirebaseAuth.DefaultInstance.

Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;

Authenticate with Firebase

  1. Follow instructions for Android and iOS+to get a token for the signed-in GitHub user.
  2. After a user successfully signs in, exchange the access token for a Firebase credential, and authenticate with Firebase using the Firebase credential:
    Firebase.Auth.Credential credential =
    Firebase.Auth.GitHubAuthProvider.GetCredential(accessToken);
    auth.SignInAndRetrieveDataWithCredentialAsync(credential).ContinueWith(task => {
    if (task.IsCanceled) {
    Debug.LogError("SignInAndRetrieveDataWithCredentialAsync was canceled.");
    return;
    }
    if (task.IsFaulted) {
    Debug.LogError("SignInAndRetrieveDataWithCredentialAsync encountered an error: " + task.Exception);
    return;
    }
    Firebase.Auth.AuthResult result = task.Result;
    Debug.LogFormat("User signed in successfully: {0} ({1})",
    result.User.DisplayName, result.User.UserId);
    });

Next Steps

After a user signs in for the first time, a new user account is created and linked to the credentials—that is, the user name and password, phone number, or auth provider information—the user signed in with. This new 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 how the user signs in.

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():

auth.SignOut();