Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call firebase callable functions from localhost?

I get this error:

enter image description here

whenever this cloud function is called:

      const makeAdmin = firebase.functions().httpsCallable("makeAdmin");
      makeAdmin({
        make: "admin"
      })
        .then(response => {
          console.log(response);
        })
        .catch(err => console.error(err));

the function is:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();
const auth = admin.auth();

exports.makeAdmin = functions.https.onCall(async (data, context) => {
  try {
    const email = context.auth.token.email || null;
    const user = await auth.getUserByEmail(email);
    await auth.setCustomUserClaims(user.uid, {
      admin: true
    });
    return {
      message: "admin added successfully"
    };
  } catch (error) {
    return error;
  }
});

I tried using cors module but didn't work.

like image 816
Touha Avatar asked Aug 15 '26 06:08

Touha


1 Answers

You need to enable cors, to be able to do that request:

First install the package:

npm install cors

Then import it:

const cors = require('cors')({origin: true});
like image 127
Peter Haddad Avatar answered Aug 17 '26 20:08

Peter Haddad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!