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

Viewed 1148

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?

2 Answers

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);
Related