Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gregfenton/231da6739d3ea71da6a54adbdc082cff to your computer and use it in GitHub Desktop.
Save gregfenton/231da6739d3ea71da6a54adbdc082cff to your computer and use it in GitHub Desktop.

QUESTION ASKED OF THE CHAT-GPT-THINGY:


How can i get a unity game to log into Firebase Authentication that has an integration to Epic's OpenID connector?

RESPONSE FROM THE CHAT-GPTs:


To integrate Firebase Authentication with Epic Games’ OpenID connector in a Unity game, you'll need to set up Firebase Authentication for custom authentication and use Epic Games as an external identity provider. Here’s a step-by-step guide on how to do it.

1. Set Up Firebase Authentication

First, set up Firebase Authentication for your project:

  1. Create a Firebase Project:

  2. Add Unity Project to Firebase:

    • Follow the steps in the Firebase Console to add your Unity project to Firebase.
    • Download and add the google-services.json (for Android) and GoogleService-Info.plist (for iOS) files to the respective directories in your Unity project.
  3. Enable Authentication:

    • In the Firebase Console, go to Authentication > Sign-in method and enable Anonymous or Custom Authentication.
    • Custom authentication is necessary if you’re handling the token issuance through Epic’s OpenID Connect service.
  4. Install Firebase SDK for Unity:

2. Set Up Epic Online Services (EOS)

Epic provides OAuth 2.0 and OpenID Connect as part of its online services. Here’s how to set up EOS to integrate OpenID Connect.

  1. Create an Epic Developer Account and register your app with Epic Online Services if you haven't done so already.

  2. Set Up OpenID Connect in EOS:

    • In the Epic Games Developer Portal, go to your project and enable OpenID Connect under OAuth2 Settings.
    • Record your Client ID, Client Secret, and Issuer URL from the settings. You’ll need these to configure your OpenID flow.
  3. Redirect URI:

    • Make sure you specify a redirect URI in EOS that will work for Firebase. This is typically set in your Firebase Console as well.

3. Implement Authentication Flow in Unity

In your Unity project:

  1. Initialize Firebase in Unity:

    • Make sure Firebase is initialized in your Unity project.
    using Firebase;
    using Firebase.Auth;
    using UnityEngine;
    
    public class FirebaseInitializer : MonoBehaviour
    {
        void Start()
        {
            FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
            {
                if (task.Result == DependencyStatus.Available)
                {
                    // Initialize Firebase
                    Debug.Log("Firebase is ready!");
                }
                else
                {
                    Debug.LogError("Could not resolve Firebase dependencies: " + task.Result);
                }
            });
        }
    }
  2. Handle Epic Games Authentication:

    • Use the EOS SDK to initiate the login flow. After successfully logging in with Epic, retrieve the OpenID token from EOS.
    using Epic.OnlineServices;
    using Epic.OnlineServices.Auth;
    
    public class EpicAuth : MonoBehaviour
    {
        private void LoginWithEpic()
        {
            AuthInterface authInterface = EOSPlatform.Instance.GetAuthInterface();
            authInterface.Login(new LoginOptions
            {
                Credentials = new Credentials
                {
                    Type = AuthCredentialType.ExternalAuth,
                    ExternalType = ExternalCredentialType.EpicGames // Specifying the OpenID connection
                }
            }, null, OnEpicLoginCallback);
        }
    
        private void OnEpicLoginCallback(LoginCallbackInfo data)
        {
            if (data.ResultCode == Result.Success)
            {
                // Successfully authenticated with Epic
                string openIdToken = data.AccountToken; // Use this for Firebase authentication
                SignInToFirebase(openIdToken);
            }
            else
            {
                Debug.LogError("Failed to login with Epic: " + data.ResultCode);
            }
        }
    }
  3. Sign In to Firebase with OpenID Token:

    • Use Firebase’s SignInWithCustomTokenAsync method to authenticate with Firebase using the OpenID token from Epic.
    private FirebaseAuth auth;
    
    private void SignInToFirebase(string openIdToken)
    {
        auth = FirebaseAuth.DefaultInstance;
    
        // Exchange OpenID token with Firebase Custom Token (if necessary)
        auth.SignInWithCustomTokenAsync(openIdToken).ContinueWith(task =>
        {
            if (task.IsCanceled)
            {
                Debug.LogError("SignInWithCustomTokenAsync was canceled.");
                return;
            }
            if (task.IsFaulted)
            {
                Debug.LogError("SignInWithCustomTokenAsync encountered an error: " + task.Exception);
                return;
            }
    
            FirebaseUser newUser = task.Result;
            Debug.LogFormat("User signed in successfully: {0} ({1})",
                newUser.DisplayName, newUser.UserId);
        });
    }

    Note: Depending on your setup, you may need to exchange the OpenID token from Epic with a Firebase-compatible custom token. This exchange is often done through a backend server that communicates with both Epic Games' and Firebase's APIs.

4. Test the Integration

Run your Unity game and attempt to sign in using the Epic Games credentials. You should see the successful authentication messages in Unity's Console when the login is complete.


This setup allows your Unity app to leverage Epic's OpenID as an authentication provider through Firebase Authentication, creating a seamless integration for users who log in via their Epic accounts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment