1

My mobile app built in Flutter uses google login to register users. From within this app I am calling a Firebase cloud function (called questionAnswer) using the Cloud Functions Plugin for Flutter.

If I understand correctly from this documentation, the https request should automatically include the Firebase authentication of the user.

How can I retrieve the authentication information of the user from within the Cloud Function? I need it in order to access data associated with that specific user in a Firebase Cloud Database. Should I include the google auth token as a parameter in the https request?

Here is my code in the Flutter app:

final HttpsCallable callable = CloudFunctions.instance.getHttpsCallable(
      functionName: 'questionAnswer',
  );

 fetchHttps() async {
   dynamic resp = await callable.call();
   print(resp);
 }

This is the code in the Cloud Function

exports.questionAnswer = functions.https.onCall(() => {
  console.log('addNumbersLog', "holaLog");
    return answer;
  });

2 Answers 2

1

As you can see from the documentation, auth information is in the second parameter passed to the function. You will need to declare and use it:

exports.questionAnswer = functions.https.onCall((data, context) => {
  console.log('addNumbersLog', "holaLog");
  console.log(context.auth);
  return answer;
});

context here is a CallableContext object.

Sign up to request clarification or add additional context in comments.

Comments

1

You can obtain the uid of the user from the CallableContext parameter, which is passed to your onCall handler as the second argument.

From there on you can retrieve the Firebase UserRecord using the .getUser() method and passing in the uid.

exports.questionAnswer = functions.https.onCall((data, { auth }) => {
    admin.auth().getUser(auth.uid)
      .then((userRecord) => {
        // See the UserRecord reference doc for the contents of userRecord.
        console.log('Successfully fetched user data:', userRecord.toJSON());
      })
      .catch(function(error) {
        console.log('Error fetching user data:', error);
      });
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.