0

I want to delete by facebook provider user in my firebase authentication using this question in my nodeJs. But somehow i getting error that getUserByProviderUid is not a function. I attached the error message below.

enter image description here

Here is code

exports.deleteUserData = functions.https.onRequest(async (req, 
res) => {
  try {
  const signedRequest = req.body.signed_request;
  console.log('signRewues',signedRequest)
  const userObj = parseSignedRequest(signedRequest, 
  'Facebook secret');
  console.log('User Obj',userObj, userObj.user_id);
  const userRecord = await 
  admin.auth().getUserByProviderUid("facebook.com", 
  userObj.user_id);
  console.log('userRecord',userRecord);
  await admin.auth().deleteUser(userRecord.uid);
  res.json({
    url: "<URL>",
    confirmation_code: "<Code>",
  });
  } catch (e) {
  console.log(e);
  res.status(400).json({
    message: "Bad Request",
  });
  }
});

function base64decode(data) {
   while (data.length % 4 !== 0) {
    data += "=";
   }
   data = data.replace(/-/g, "+").replace(/_/g, "/");
   return Buffer.from(data, "base64").toString("utf-8");
};

function parseSignedRequest(signedRequest, secret) {
   var encoded_data = signedRequest.split(".", 2);
   // decode the data
   var sig = encoded_data[0];
   var json = base64decode(encoded_data[1]);
   var data = JSON.parse(json);
  if (!data.algorithm || data.algorithm.toUpperCase() != "HMAC- 
  SHA256") {
    throw Error(
    "Unknown algorithm: " + data.algorithm + ". Expected HMAC- 
  SHA256"
  );
  }
  var expected_sig = crypto
    .createHmac("sha256", secret)
    .update(encoded_data[1])
    .digest("base64")
    .replace(/\+/g, "-")
    .replace(/\//g, "_")
    .replace("=", "");
  if (sig !== expected_sig) {
    throw Error("Invalid signature: " + sig + ". Expected " + 
    expected_sig);
  }
  return data;
}
Ahsan Ullah
  • 158
  • 4
  • 15

1 Answers1

3

The getUserByProviderUid() was added in version 9.5.0 of Admin SDK. You'll have to update the Admin SDK to use it. Try upgrading to latest version.

npm i firebase-admin@latest
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • Thank you @Dharmaraj. One thing i want to that now it work fine but when i click the send send request button in facebook it show success message like **Request received by abc. View the status of your request using this confirmation number: undefined**. as i am already send confirmation number in res.json. And could you please specify we what is confirmation_code should and what url is used for? – Ahsan Ullah Feb 17 '22 at 09:18
  • I'm not really sure how is `confirmation_code` being used after the response is sent. Can you provide more details on it? – Dharmaraj Feb 17 '22 at 10:30