Home > Net >  firebase admin.auth().getUserByProviderUid is not a function
firebase admin.auth().getUserByProviderUid is not a function

Time:02-18

I want to delete by facebook provider user in my firebase authentication using 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;
}

CodePudding user response:

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
  • Related