How to make FirebaseFunctions.instance.httpsCallable() pass Authorization header? (Using Flutter)

Viewed 521

I'm struggling with getting the user id token included in the call to httpsCallable(). I call it like this from flutter:

Future<bool> cfCreateAccount(String name, address, email, phoneNumber, password, randomSalt, {int? parentId}) async {
  HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('createAccount', options: HttpsCallableOptions(timeout: Duration(seconds: 10)));
  try {
    var param = {'name': name, 'address': address, 'email': email, 'phone_number': phoneNumber, 'password': password, 'random_salt': randomSalt};
    final HttpsCallableResult result = await callable.call(param);
    if (result.data['response'] == 'Pass') {
      return true;
    }
  } catch (e) {
    print("Failed Cloud Function Call: $e");
  }
  return false;
}

This person was asking I think a similar question but for them they said the answer was Authorization was automatically included:

How to add headers to firebase httpscallable

I've signed in in my Flutter app with this:

await auth.signInWithEmailAndPassword(email: user, password: password);

and afterwards I can call user.getIdToken() and it shows what looks like my auth id. But every way I try to check seems to indicate that the auth header isn't in the request to the cloud function.

I wrote the cloud function in python. It looks like this:

def createAccount(request):
    request_json = request.get_json(silent=True)
    request_args = request.args
    sql = None
    conn = None
    res = dict()

    res['data'] = str(request.authorization)
    return json.dumps(res)

But this returns "None". Meaning there is no authorization in the request. I also tried this but it gives an error in python:

token = request.headers['Authorization'].replace('Bearer ','')

Is there some issue maybe with how I call httpsCallable() in flutter? I'm not sure how FirebaseFunctions.instance.httpsCallable() gets the auth id from FirebaseAuth.instance ??

Here is the cloud function on the Firebase console, just to show that it is there and in python3.8:

1 Answers

Seems like some features of Firebase Cloud Functions aren't available unless you use the Firebase CLI to deploy them. The CLI doesn't support python, only Javascript and Typescript. I'd recommend rewriting the function in either of those languages and deploying it through the Firebase CLI.

There's a similar answer here: https://stackoverflow.com/a/54583565/3822043

Related