Home > Software design >  Can you use Unity / Firebase Auth with C# google cloud functions?
Can you use Unity / Firebase Auth with C# google cloud functions?

Time:10-17

I'm looking into porting a unity game with a dotnet backend over to firebase firestore / functions, etc. My initial tests look promising, but my biggest hold up is that I would need to rewrite a lot of the server side logic in JavaScript.

I know that firebase's functions and firestore both run on google cloud, and the cloud version of functions supports a number of additional languages, including c#. I was able to create a couple of C# test functions and upload it to the same project as my unity test. They show up in the firebase portal and can be called via the http end points supplied by google cloud, etc.

I discovered that I can call the function that does not require auth using the FirebaseFunctions callabale api in unity (fun fact, it only seems to work if the response is a json object in the form of { "result": [data here] }) I cannot however call the version of the same function that does require authentication - it returns an internal error message.

I am wondering there is way to make these methods callable from the unity firebase api - passing in the user id / auth that I get from logging into firebase? I've seen some examples / answers where people say to call the cloud function directly using the Authorization: bearer token header, but I cannot seem to find a way to get the auth token from the current user in the Unity Firebase api.

I imagine that I am stepping further outside the realm of firesbase unity api and more into google cloud / identity platform

CodePudding user response:

You can retrieve the ID token of the current user by calling TokenAsync() on their profile.

That's the value you need to pass along in the Authorization header of the call to Cloud Functions, where you can then access it in you Callable Cloud Function.

Alternatively you can implement a regular HTTP Cloud Function, pass the same in whatever way you see fit there, and then on the server decode and verify the ID token with the Admin SDK yourself.

  • Related