Is it possible to call firebase functions with 'firebase-admin' package?
Asked Answered
K

2

6

I have node app with service account based access, so I used firebase-admin. As I could see before, firebase-admin mostly duplicated firebase package (except for authentication part, signatures and maybe some other parts), but now I want to invoke functions and I can't find any equivalent for firebase.apps[0].functions().httpsCallable('myFunction'). I looked into Typescript typings, and they don't even mention functions.

admin.initializeApp({
  credential: admin.credential.cert('./service-account-credentials.json'),
  databaseURL: process.env.REACT_APP_FIREBASE_DATABASE_URL,
});

const config = {
  storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
};

const storageBucket = admin.storage().bucket(config.storageBucket);
const firestore = admin.firestore();



// const functions = admin.apps[0].functions(); // not possible
const functions = firebase.apps[0].functions(); // possible, but Firestore.apps not initialized

What are my options?

Khmer answered 29/11, 2020 at 13:11 Comment(0)
V
7

There is no firebase-functions client for calling Callable Cloud Functions in the Node.js Admin SDK for Firebase.

It is not an unreasonable request though, so I recommend filing a feature request on the Github repo, so others can weigh in too.

In the meantime, the only option is to implement the wire protocol for callable Cloud Functions yourself, possibly based on their implementation on the regular JavaScript SDK.

Vagrom answered 29/11, 2020 at 14:51 Comment(5)
thanks for an answer, was expecting something like that. Was actually thinking about initializing another app for 'firebase' lib using email and password of firebase user.Khmer
I landed here looking for a way to use httpsCallable from my Google Cloud IoT device running Nodejs. I need to call a cloud function that makes a request to Mender to preauthorize the device on their platform. from a systemd service when the device first boots. @DougStevens mentions using a service account here, which I have, but it doesn't sound like that fixes my situation. Any new developments with this or suggested work arounds?Amble
The workaround mentioned in my answer is still the only one I know of.Vagrom
I tried running with the web sdk as it notes it is here that it is suitable for "IoT devices running Node.js." here. But it throws; menderPreAuth Error: Error: internal (node:773) UnhandledPromiseRejectionWarning: Error: internal at new HttpsErrorImpl (/data/node_modules/.pnpm/@firebase/[email protected]_e9d66a23fa701e65014ff8d410061da5/node_modules/@firebase/functions/dist/index.node.cjs.js:62:28) What is "Error: internal"?Amble
Sorry, but this is not a good place to help with that. If you have a problem implementing Cloud Functions in your app, post a new question with its own MCVE please.Vagrom
K
3

Ended up with this code. User id is firebase user uid.

const firebaseApp = async (userId: string) => {
  if (firebase.apps?.length) {
    firebase.apps.forEach((app) => app.delete());
    console.log(`Deleted ${firebase.apps.length} apps`);
  }

  const customToken = await admin.auth().createCustomToken(userId);
  const firebaseApp = firebase.initializeApp(config);

  await firebaseApp.auth().signInWithCustomToken(customToken);

  return firebaseApp;
};
const firebase = await firebaseApp('WObPvJbZDIR9WB72Tun8Jg4D0ky2');

await firebase
  .functions('europe-west1')
  .httpsCallable('myFunction')(doc);
Khmer answered 29/11, 2020 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.