(as a followup to this question):
But the Firebase SDK for Unity does not support Windows. I need to support Windows.
You're correct that the Firebase SDK for Unity does not natively support Windows Desktop. To implement Firebase Authentication with Epic's OpenID on Windows in Unity, you'll need a workaround that leverages Firebase's REST API for authentication instead. Here’s how you can set this up.
The Firebase Authentication REST API allows you to authenticate users with custom tokens, email/password, and other authentication providers (including custom OpenID providers like Epic Games) from any platform, including Windows.
To authenticate with Firebase, you'll need to use the Firebase Authentication REST API after getting the OpenID token from Epic Games.
Here’s the process:
-
Obtain an OpenID Token from Epic Games:
- Use Epic Games’ SDK to get an OpenID Connect token after the user logs in.
-
Exchange the OpenID Token for a Firebase Custom Token:
- Since Firebase Authentication needs a Firebase custom token, you’ll typically need a backend server to create this custom token.
- Your backend server will receive the OpenID token from the Unity app, validate it with Epic, and create a Firebase custom token using the Firebase Admin SDK.
-
Send Firebase Custom Token to Firebase Authentication REST API:
- Use Unity’s
UnityWebRequest
to send the custom token to Firebase Authentication, which will return an ID token and a refresh token for your Unity app to use.
- Use Unity’s
Let’s break these down in detail:
-
Firebase Console:
- Set up Firebase Authentication, as described in the previous message. Make sure Custom Authentication is enabled in Firebase.
-
Epic Games Setup:
- In Epic's developer portal, enable OpenID and obtain the necessary credentials.
The backend can be written in any language that supports Firebase’s Admin SDK. Here's a sample in Node.js:
-
Install Firebase Admin SDK:
npm install firebase-admin
-
Create a Server to Exchange OpenID Token for Firebase Custom Token:
const admin = require("firebase-admin"); // Initialize Firebase Admin SDK admin.initializeApp({ credential: admin.credential.applicationDefault() }); // Express server setup const express = require('express'); const app = express(); app.use(express.json()); app.post('/getFirebaseCustomToken', async (req, res) => { const { openIdToken } = req.body; try { // Verify OpenID token with Epic (you may need Epic's token verification endpoint here) // Create Firebase Custom Token using the unique user identifier from Epic const firebaseCustomToken = await admin.auth().createCustomToken(openIdToken); res.json({ firebaseCustomToken }); } catch (error) { console.error("Error creating custom token:", error); res.status(500).send("Error creating custom token"); } }); app.listen(3000, () => { console.log('Server running on port 3000'); });
This server receives the OpenID token from your Unity client, verifies it, and creates a Firebase custom token.
Now, in your Unity project:
-
Get the OpenID Token Using Epic Games SDK:
- Use the Epic SDK to log in and retrieve the OpenID token, as shown in the previous response.
-
Send the OpenID Token to Your Backend:
- Use
UnityWebRequest
to send a POST request to your backend server to exchange the OpenID token for a Firebase custom token.
using UnityEngine; using UnityEngine.Networking; using System.Collections; public class FirebaseAuthWithEpic : MonoBehaviour { private string backendUrl = "http://localhost:3000/getFirebaseCustomToken"; // Your backend server URL public void AuthenticateWithEpic() { StartCoroutine(GetFirebaseToken("YourEpicOpenIdToken")); } private IEnumerator GetFirebaseToken(string openIdToken) { WWWForm form = new WWWForm(); form.AddField("openIdToken", openIdToken); using (UnityWebRequest www = UnityWebRequest.Post(backendUrl, form)) { yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError("Error getting Firebase custom token: " + www.error); } else { var responseJson = www.downloadHandler.text; // Parse JSON to retrieve Firebase Custom Token string firebaseCustomToken = ParseFirebaseCustomToken(responseJson); StartCoroutine(SignInWithFirebase(firebaseCustomToken)); } } } private string ParseFirebaseCustomToken(string responseJson) { // Parse the JSON response to retrieve "firebaseCustomToken" return responseJson; // Adjust as needed for actual parsing } private IEnumerator SignInWithFirebase(string firebaseCustomToken) { string firebaseUrl = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=[YOUR_FIREBASE_WEB_API_KEY]"; WWWForm form = new WWWForm(); form.AddField("token", firebaseCustomToken); form.AddField("returnSecureToken", "true"); using (UnityWebRequest www = UnityWebRequest.Post(firebaseUrl, form)) { yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError("Firebase sign-in failed: " + www.error); } else { var responseJson = www.downloadHandler.text; Debug.Log("Firebase sign-in successful: " + responseJson); // Save Firebase ID token and refresh token for use in the game } } } }
- Note: Replace
[YOUR_FIREBASE_WEB_API_KEY]
with your actual Firebase Web API key found in the Firebase Console under Project Settings.
- Use
-
Store the Tokens:
- Upon successful sign-in, store the Firebase ID token and refresh token securely for use throughout the game session.
- Backend: Handles OpenID token verification and exchanges it for a Firebase custom token.
- Unity: Sends the OpenID token to the backend and, upon receiving the Firebase custom token, authenticates with Firebase Authentication REST API.
This setup should work for Windows, as it uses Unity's networking and Firebase's REST API, which are both platform-agnostic.