Here is the code for a working firebase cloud function and following is a question about some change I want to make and can't at this point, even after trying several options.
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import * as cors from "cors";
const corsHandler = cors({origin: true});
admin.initializeApp(functions.config().firebase);
exports.myFunc = functions.https.onRequest(function(req, resp) {
corsHandler(req, resp, async () => {
const from = String(req.query.from); // Through the URL.
// const from = req.body.from; // I tried this among other things to get the information through a POST but it did not work.
admin.auth().getUserByEmail(from)
.then(function(userRecord) {
console.log("Successfully fetched user data:",
userRecord.toJSON());
})
.catch(function(error) {
console.log("Error fetching user data:", error);
});
});
});
This function can be called using a URL like:
https://us-central1-myapp.cloudfunctions.net/myFunc?from=example@example.com
But what I want is to be able to call it through a POST from a JS page (with XMLHttpRequest) in my web app.
How should I change the function above for that?
For reference, this is the kind of code I am trying to use on the web-app side to call the cloud function (but it is not working):
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://us-central1-myapp.cloudfunctions.net/myFunc", true, 'me@example.com', 'VerySecretPassWord');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send({"from": "example@example.com"});
xhr.onload = function() {
var data = JSON.parse(this.responseText);
console.log('THE DATA:',data);
};