Search code examples
node.jsfirebasegithubherokufirebase-admin

Firbase/Heroku/Node JS - Firebase Invalid Credential / failed to parse service account json file / ENAMETOOLONG


I'm using github actions to deploy my first webapp via Heroku.

My code:

const fireBaseAdmin = require('firebase-admin');

if (!fireBaseAdmin) 
  throw new Error('The FIREBASE_SERVICE_ACCOUNT_CREDS environment variable was not found!');

fireBaseAdmin.initializeApp({
  "credential": fireBaseAdmin.credential.cert(JSON.stringify({
    "type": process.env.FIREBASE_TYPE,
    "project_id": process.env.FIREBASE_PROJECT_ID,
    "private_key_id": process.env.FIREBASE_PRIVATE_KEY_ID,
    "private_key": process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
    "client_email": process.env.FIREBASE_CLIENT_EMAIL,
    "client_id": process.env.FIREBASE_CLIENT_ID,
    "auth_uri": process.env.FIREBASE_AUTH_URI,
    "token_uri": process.env.FIREBASE_TOKEN_URI,
    "auth_provider_x509_cert_url": process.env.FIREBASE_AUTH_PROVIDER,
    "client_x509_cert_url": process.env.FIREBASE_CLIENT
  }))
});

module.exports = fireBaseAdmin;

Each of the .env variables are stored in Heroku's Config Vars.

Error log from Heroku (I stripped sensitive tokens/keys):

2021-08-17T11:16:53.818119+00:00 app[web.1]: /app/node_modules/firebase-admin/lib/credential/credential-internal.js:151
2021-08-17T11:16:53.818149+00:00 app[web.1]: throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse service account json file: ' + error);
2021-08-17T11:16:53.818150+00:00 app[web.1]: ^
2021-08-17T11:16:53.818150+00:00 app[web.1]: 
2021-08-17T11:16:53.818166+00:00 app[web.1]: FirebaseAppError: Failed to parse service account json file: Error: ENAMETOOLONG: name too long, open '{
"type":"service_account",
"project_id":"ec31",
"private_key_id":"2900d",
"private_key":"-----BEGIN PRIVATE KEY-----\nQ=\n-----END\nPRIVATE KEY-----\n",
"client_email":"firebase-adminsdk-.gserviceaccount.com",
"client_id":"10",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url":"ttps://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-o.gserviceaccount.com"
}'
2021-08-17T11:16:53.818167+00:00 app[web.1]: at FirebaseAppError.FirebaseError [as constructor] (/app/node_modules/firebase-admin/lib/utils/error.js:44:28)
2021-08-17T11:16:53.818168+00:00 app[web.1]: at FirebaseAppError.PrefixedFirebaseError [as constructor] (/app/node_modules/firebase-admin/lib/utils/error.js:90:28)
2021-08-17T11:16:53.818168+00:00 app[web.1]: at new FirebaseAppError (/app/node_modules/firebase-admin/lib/utils/error.js:125:28)
2021-08-17T11:16:53.818169+00:00 app[web.1]: at Function.ServiceAccount.fromPath (/app/node_modules/firebase-admin/lib/credential/credential-internal.js:151:19)
2021-08-17T11:16:53.818169+00:00 app[web.1]: at new ServiceAccountCredential (/app/node_modules/firebase-admin/lib/credential/credential-internal.js:67:28)
2021-08-17T11:16:53.818174+00:00 app[web.1]: at Object.exports.cert (/app/node_modules/firebase-admin/lib/credential/credential.js:34:54)
2021-08-17T11:16:53.818174+00:00 app[web.1]: at Object.<anonymous> (/app/firebase/index.js:25:42)
2021-08-17T11:16:53.818175+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1072:14)
2021-08-17T11:16:53.818175+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
2021-08-17T11:16:53.818175+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:937:32)
2021-08-17T11:16:53.818176+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:778:12)
2021-08-17T11:16:53.818176+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:961:19)
2021-08-17T11:16:53.818177+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:92:18)
2021-08-17T11:16:53.818177+00:00 app[web.1]: at Object.<anonymous> (/app/middlewares/auth.js:1:15)
2021-08-17T11:16:53.818177+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1072:14)
2021-08-17T11:16:53.818178+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10) {
2021-08-17T11:16:53.818178+00:00 app[web.1]: errorInfo: {
2021-08-17T11:16:53.818178+00:00 app[web.1]: code: 'app/invalid-credential',
2021-08-17T11:16:53.818190+00:00 app[web.1]: message: `Failed to parse service account json file: Error: ENAMETOOLONG: name too long, open '{
"type":"service_account",
"project_id":"e1",
"private_key_id":"2900",
"private_key":"-----BEGIN PRIVATE KEY-----\\nMIIEMoQ=\\n-----END\\nPRIVATE KEY-----\\n",
"client_email":"firebase-adminsdk-od.gserviceaccount.com",
"client_id":"1",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url":"ttps://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-o.gserviceaccount.com"
}'`
2021-08-17T11:16:53.818190+00:00 app[web.1]: },
2021-08-17T11:16:53.818190+00:00 app[web.1]: codePrefix: 'app'
2021-08-17T11:16:53.818191+00:00 app[web.1]: }
2021-08-17T11:16:53.947406+00:00 heroku[web.1]: Process exited with status 1
2021-08-17T11:16:54.345497+00:00 heroku[web.1]: State changed from starting to crashed

I've been attempted to update my code based on the various posts on Stack Overflow but I'm not having success with solving the error. Here's a short list of the post I've been referencing:

Deploying Firebase App with Service Account to Heroku (environment variables with dotenv) Authenticating with the Firebase Admin SDK using environment variable Node.js -Firebase Service Account Private Key won't parse Firebase: Failed to parse service account: 'project_id' must be set

Any assistance would be greatly appreciated!


Solution

  • The cert() method expects either the path to serviceAccount.json file (string) or a ServiceAccount object. Here you are entering a string so it's expecting that to be the path to serviceAccount.json and not stringified credentials and hence you get the error FirebaseAppError: Failed to parse service account json file. Try this:

    fireBaseAdmin.initializeApp({
      credential: admin.credential.cert({
        type: process.env.FIREBASE_TYPE,
        project_id: process.env.FIREBASE_PROJECT_ID,
        private_key_id: process.env.FIREBASE_PRIVATE_KEY_ID,
        private_key: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
        client_email: process.env.FIREBASE_CLIENT_EMAIL,
        client_id: process.env.FIREBASE_CLIENT_ID,
        auth_uri: process.env.FIREBASE_AUTH_URI,
        token_uri: process.env.FIREBASE_TOKEN_URI,
        auth_provider_x509_cert_url: process.env.FIREBASE_AUTH_PROVIDER,
        client_x509_cert_url: process.env.FIREBASE_CLIENT
      }),
      databaseURL: `https://${process.env.FIREBASE_PROJECT_ID}.firebaseio.com/`
    })