CodingIsFun33
CodingIsFun33

Reputation: 453

JSON string sending undefined when trying to set custom claim via firebase function

Can anyone see what I am doing wrong here? I am using firebase, I am sending data to my cloud function here

window.addEventListener("load", function load(event) {

    firebase.auth().onAuthStateChanged(function (user) {
        if (user) {
            let user = firebase.auth().currentUser;
            let userInfo = db.collection("Users").doc(user.uid);



            userInfo.get().then(function (doc) {
                if (doc.exists) {

                    let userEmail = firebase.auth().currentUser.email
                    var xhttp = new XMLHttpRequest();

                    xhttp.open(
                        "POST",
                        "https://us-central1-******.cloudfunctions.net/addMRole",
                        true
                    );
                    xhttp.setRequestHeader("Content-Type", "application/json");
                    xhttp.send(
                        JSON.stringify({
                            email: userEmail,
                            uID: user.uid
                        })
                    );
                    console.log("should still be working HOPEFULLY HERE YOU ARE")


                }
            });

        }

    });

});

and I am sending it to my cloud function here

exports.addMRole = functions.https.onRequest((req, context) => {

  let email = req.email;
  let uiD = req.uID

  console.log(email)
  console.log(uiD)

  return admin.auth().getUserByEmail(email).then(user => {

    return admin.auth().setCustomUserClaims(uiD, {

      member: true


    });
  }).then(() => {
    return {
      message: `success! ${req.email} is now a member!`
    }
  }).catch(err => {
    console.log(err)
    return err;
  });
});




But for some reason it is being sent as undefined to the cloud function for some reason. It does not like how im sending it to it I guess. It looks like its not happy with home im sending it from the first code here


xhttp.send(
                        JSON.stringify({
                            email: userEmail,
                            uID: user.uid
                        })
                    );

And userEmail / user.uid console log fine before they send, but then in the function when I log here

console.log(email)
  console.log(uiD)

its shows its being sent as undefined in my function logs. So confusing lol. I am basically sending the users email and UID to my cloud function which I want to change their custom claim to member. Its weird because I had it working, then I did something and now cant figure out how to get it working again, no idea where I messed up on this one lol. Thanks for the help <3

Upvotes: 1

Views: 190

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317497

This is not right:

let email = req.email
let uiD = req.uID

These will always be undefined, the way you have it written now. If you want to access the properties of a JSON object that you post to an HTTP function, you should do it like this, as shown in the documentation:

let email = req.body.email
let uiD = req.body.uID

On top of that, your function is also not sending a response to the caller. As structured right now, it might do its work, but it will always time out after the default 60 seconds. The way you are just returning the response out of the then callback will not work. It seems you might be mixing up HTTP type triggers with callable type triggers, which have very different ways of sending the response. Since you are writing an HTTP type trigger, you should follow the instructions for sending a response in order to terminate the HTTP function correctly.

Upvotes: 2

Related Questions