Search code examples
node.jsfirebasefirebase-realtime-databasegoogle-cloud-functionsprogressive-web-apps

Firebase onCall function return Error with 4G network but works with Wifi


On my PWA, there is some functions running by onCall functions. It was working properly, but some days ago any of these functions is returning "Firebase error: internal" only when the PWA is running on a mobile with 4g network. In a Wifi network everything is working as expected.

Some extra informations:

  1. When in 4g, the function return the error, but without any message log on firebase console. Even "Function execution started" or "Function execution took..." is displayed.
  2. Yes my 4g is ok. Despite this onCall functions error, others parts that demands connection is working fine (for example: any database call from SDK firebase)
  3. It's not a specific problem with my device. In some devices it works, in others no... I tested with differents devices with IOS or Android.

My code - Web:

async checkFirstLogin(uid) {
    this.apiPortal = this.functions.httpsCallable('apiPortal')
    let body = {
      action: 'checkFirstLogin',       
      uid: uid
    }
    return this.apiPortal({body: body})
    .then((response) => {
      return response.data.success
    }).catch((e) => {
      console.log('========> Error', e) //returns "Firebase error: internal"
    })
}

My Code - function

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

try {
    admin.initializeApp();
} catch (e) {}

const runtimeOpts = {
    timeoutSeconds: 60,
    memory: '2GB'
}

exports.default = functions.region('southamerica-east1').runWith(runtimeOpts).https.onCall((data, context) => {
    if (!context.auth) {
        return {
            status: 'error', 
            code: 401, 
            message: 'Not signed in'
        }
    }
    functions.logger.log(`#apiPortal - starting - action:${data.body.action}`)
    if (data.body.action==='checkFirstLogin') {
        let ref = admin.database().ref(`login/${data.body.uid}`)
        return ref.once('value').then((data) => { 
          if (data.val()) {
            let usr = data.val()
            return {success: true, data: usr.hasOwnProperty('last_login')} 
          } else {
            return {success: true, data: null}
          }
        }).catch((e) => {
          throw new functions.https.HttpsError('Unknown error', e.message, e)
        })
    } else {
        return {success: false, data: null}
    }
  }
})

Solution

  • We found that the problem is specifically with TIM 4g network. It s a DNS problem of them that do not resolve the url of our API in GCP. Today, after 3 days of problems, it's working again