Firebase-functions: Functions timeout due to warning FIREBASE WARNING: Provided authentication credentials for the app named "[DEFAULT]" are invalid. This usually indicates your app was not initialized correctly. Make sure the "credential" property provided to initializeApp() is authorized to access the specified "databaseURL" and is from the correct project.

Created on 21 Nov 2017  路  11Comments  路  Source: firebase/firebase-functions

Version info

firebase-functions:
^0.7.3
firebase-tools:
3.15.2
firebase-admin:
^5.5.0

package.json

{
  "name": "backend",
  "dependencies": {
    "@google-cloud/datastore": "^1.1.0",
    "@google-cloud/pubsub": "^0.15.0",
    "amplitude": "^3.5.0",
    "bluebird": "^3.4.7",
    "express": "^4.15.3",
    "firebase-admin": "^5.5.0",
    "firebase-functions": "^0.7.3",
    "jimp": "^0.2.27",
    "moment": "^2.17.1",
    "promise-retry": "^1.1.1",
    "request": "^2.81.0",
    "request-promise": "^4.2.1"
  },
  "private": true
}

Test case

Steps to reproduce

When a function that uses admin.database() is called, we see this warning in the logs:

FIREBASE WARNING: Provided authentication credentials for the app named "[DEFAULT]" are invalid. This usually indicates your app was not initialized correctly. Make sure the "credential" property provided to initializeApp() is authorized to access the specified "databaseURL" and is from the correct project. 

And the function timeouts after 60s.
Happens only in production.

Were you able to successfully deploy your functions?

Yes

Most helpful comment

Same issue here. Only when running the shell locally. It works when functions are deployed.

All 11 comments

cc @laurenzlong

cc @smkhalsa. Can you also provide the information for your case?

@aminelaadhari, some questions:
1) When did you first start using functions with this project? Do you remember the approximate month?
2) Can you share the code snippet for how you are using firebase-admin (including how the app was initialized, and what you are doing with admin.database())

@laurenzlong sure

I'm using firebase functions as part of my user subscription flow. I've been using this function successfully for months, but a couple days ago it started to error out with no change on my end. Here's the code for my function.

const Stripe = require('stripe')
const { admin, functions } = require('../config')

module.exports = functions.database.ref('userSubscriptions/v1-0/{userId}/stripe/customerId').onWrite(event => {
  const secretKey = functions.config().stripe.secret_key
  const stripe = Stripe(secretKey)
  const customerId = event.data.val()
  return new Promise((resolve, reject) => {
    stripe.customers.retrieve(customerId, (err, customer) => {
      if (err) return reject(err)
      return resolve(customer.subscriptions.data
        .sort((a, b) => b.current_period_end - a.current_period_end)[0] || { plan: {} })
    })
  })
  .then(activeSubscription => admin.database().ref('/userSubscriptions/v1-0')
    .child(event.params.userId)
    .child('stripe')
    .update({
      id: activeSubscription.id || null,
      validUntil: activeSubscription.current_period_end * 1000 || null, // stripe timestamps are in seconds
      planId: activeSubscription.plan.id || null,
    }))
  .then(() => console.log('Successfully updated Stripe Subscription Data!'))
})

and this was my config file:

const functions = require('firebase-functions')
const admin = require('firebase-admin')

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

module.exports = {
  admin: admin,
  functions: functions,
}

NOTE: Since this is a critical piece of my subscription flow, I've updated my config file to use a specified credential instead of functions.config().firebase. This resolved the issue. However, I'd prefer it if I could get functions.config().firebase working again.

same here the function was working fine and then stopped working. I don't if it was caused by a new deploy or no. Stackdriver error reporting wasn't able to detect the error because it's a warning and the timeout is just an info message.

We initialize the admin like this:

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

Example of usage:

return admin.database().ref('purchase/' + orderId).transaction(function (currentData) {
        if (currentData === null) {
            return req.body;
        } else {
            console.log('Purchase already exists.');
            return; // Abort the transaction.
        }
    }, function (error, committed) {
        if (error) {
            console.log('Transaction failed abnormally', error);
        } else if (!committed) {
            console.log('We aborted the transaction (because purchase already exists) orderId=[' + orderId + ']');
        } else {
            console.log('Purchase added orderId=[' + orderId + ']');
        }
        res.end();
    });

We used this workaround to get the functions work again:

admin.initializeApp({
  credential: admin.credential.cert({
    projectId: process.env.GCLOUD_PROJECT,
    clientEmail: functions.config().admin.client_email,
    privateKey: functions.config().admin.private_key
  }),
  databaseURL: functions.config().admin.database_url
});

Hi,

I also experienced something similar to this,
I had some functions deployed and working fine until a few days ago and suddenly we experienced a function timeout without any changes to firebase function or function configs. There it hung on reading and writing part of the DB. Earlier we used firebase-admin 4 version.

Then we upgrade all firebase npm packages to latest (firebase, firebase-admin, firebase-functions) and then we got this same DEFAULT credential issue and had to use service account instead of "admin.initializeApp(functions.config().firebase);"

Will this be fixed or do we need to use service account in future too?
But anyhow we have same functions with firebase-admin v4 and initialize with "admin.initializeApp(functions.config().firebase);" which working fine

Thanks
Thilina

Sorry this fell through the cracks. This seems like a pretty concerning production issue. I'm going to close this issue. if you're still facing this issue please file a ticket and I will treat it as urgent

This is technically the proper procedure for production issues (this GitHub repro's issues should be confined to SDK bugs) and I think it's best we follow protocol here. It will help us do proper escalation/handoff between teams at Google and will give us a place where we can ask for PII needed to debug the issue without you publishing it online.

I am still facing this same issue running with latest of all libraries.

"firebase-admin": "^5.5.1",
"firebase-functions": "^0.7.3"

Same issue here. Only when running the shell locally. It works when functions are deployed.

same issue

@stianlp This issue is about production, if you're seeing this in the shell, can you file another issue, and make sure to include your code that uses firebase-admin, so I can see what service might be causing that error?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TomClarkson picture TomClarkson  路  5Comments

rhodgkins picture rhodgkins  路  5Comments

h36ahmed picture h36ahmed  路  5Comments

tianhaoz95 picture tianhaoz95  路  4Comments

jyosh picture jyosh  路  3Comments