Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Functions: setup Admin Sdk without service-account.json file

I'm trying to deploy my functions on firebase without needing to add service-account.json file to my project. This gives me the following error randomly on some functions while deploying:

Error in the build environment

The solution I'm using is given in firebase docs to use

admin.initializeApp(functions.config().firebase);

And if the deploy is successful for all the functions using the above initialization, then it fails when I try to mint a custom token in m y function using admin sdk with following error:

Error: createCustomToken() requires a certificate with "private_key" set.
    at FirebaseAuthError.Error (native)
    at FirebaseAuthError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
    at FirebaseAuthError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28)

Is there any problem with using this solution? Or do we need to include the service-account.json file if we need to mint custom token?

like image 664
Umar Hussain Avatar asked Aug 31 '25 22:08

Umar Hussain


1 Answers

I had the same issue and there is a way to bypass having an explicit json file if you have few environment variables.

Initialize it like this :

 admin.initializeApp({
  credential: admin.credential.cert({
    projectId: process.env.FIREBASE_PROJECT_ID,
    clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
    privateKey: process.env.FIREBASE_PRIVATE_KEY?.replace(/\\n/g, '\n'),
    }),
  })

the last .replace is required to convert the key to how firebase accepts.

Source of my answer is this.

like image 119
srihari ayapilla Avatar answered Sep 03 '25 11:09

srihari ayapilla