firebase / firebase-unity-sdk

The Firebase SDK for Unity
http://firebase.google.com
Apache License 2.0
234 stars 38 forks source link

[Question] Functions gen2 requests not authenticated #1120

Closed aixaCode closed 2 months ago

aixaCode commented 2 months ago

What is your question?

Hi,

We recently migrated to Gen 2 Firebase functions, and while the migration itself went smoothly, we're now encountering an issue when calling these functions from Unity. The following warning appears, preventing the functions from being executed:

The function must be called while authenticated.

We do not want to allow unauthenticated access to our service, as we rely on Firebase Auth for user authentication. All users interacting with our app are authenticated via Firebase Auth, so we expected this to work without issues. Could you advise on how we can ensure proper authentication of end users while resolving this warning?

We basically call it the same way as it was provided in the example here https://github.com/firebase/firebase-unity-sdk/issues/1113#issuecomment-2367788596

Firebase Unity SDK Version

12.1.0

Unity editor version

2022.3.4.47

Installation Method

.unitypackage

Problematic Firebase Component(s)

Functions

Other Firebase Component(s) in use

No response

Additional SDKs you are using

No response

Targeted Platform(s)

Apple Platforms, Android, Desktop

Unity editor platform

Windows

Scripting Runtime

IL2CPP

Release Distribution Type

Pre-built SDK from https://firebase.google.com/download/unity

google-oss-bot commented 2 months ago

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

argzdev commented 2 months ago

Hey @aixaCode, thanks for reaching out. Not sure if your issue has been resolved, but in case it hasn't, you would simply need to add and make sure that your user has been authenticated before firing any functions call. I'm guessing you're running into a race condition where you're likely sending your function calls too early.

Here's an example for calling Auth from our Firebase Unity Quickstarts:

Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
    dependencyStatus = task.Result;
    if (dependencyStatus == Firebase.DependencyStatus.Available) {
      SigninAnonymouslyAsync();
    } else {
      Debug.LogError(
        "Could not resolve all Firebase dependencies: " + dependencyStatus);
    }
  });

public Task SigninAnonymouslyAsync() {
  DebugLog("Attempting to sign anonymously...");
  DisableUI();
  return auth.SignInAnonymouslyAsync().ContinueWithOnMainThread(HandleSignInWithAuthResult);
}

After this, you can then make Cloud Function calls on HandleSignInWithAuthResult. This is only a partial pseudocode. Feel free to check out our Firebase Unity Auth source code for reference.