4

I have set up a cloud function in my Firebase console with the following inside the index.js.

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

const cors = require('cors');

const serviceAccount = require("./serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://**********.firebaseio.com"
});

const authCheck = (req, res) => {
  cors(req, res, () => {
    const tokenId = req.get('Authorization').split('Bearer ')[1];

    return admin.auth().verifyIdToken(tokenId)
      .then((decoded) => res.status(200).send(decoded))
      .catch((error) => res.status(401).send(error));
  });
};

module.exports = {
  authCheck: functions.https.onRequest(authCheck),
};

Whenever I try to access to the URL of this HTTPS endpoint, the cloud function always times out. I'm not able to debug what the issue is because the log in the console is in the form of blank lines.

enter image description here

Utkarsh Bhatt
  • 1,536
  • 2
  • 14
  • 23

1 Answers1

1

Firebase provide an example cloud function on Github which is worth looking at - it imports cors. It imports them with an additional argument;

const cors = require('cors')({
  origin: true,
});

I haven't seen cloud functions make use of module.exports - It could be valid, but I just haven't seen it so a refactor of your code could look something like this;

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

// You don't need to import the JSON file - Firebase has it's creds stored in `functions.config().firebase`
admin.initializeApp(functions.config().firebase);

const cors = require('cors')({ origin: true });

exports.authCheck = functions.https.onRequest((req, res) => {
  return cors(req, res, () => {
    const tokenId = req.get('Authorization').split('Bearer ')[1];

    admin.auth().verifyIdToken(tokenId)
      .then((decoded) => {
        console.log('Decoded', decoded);
        res.status(200).send(decoded);
      })
      .catch((error) => {
        console.error('Catching Error', error);
        res.status(401).send(error)
      });
  });
});

If you run that does it help with the logging?

sketchthat
  • 2,678
  • 2
  • 13
  • 22
  • module.exports works fine. But, my question was not about that. I have tried your code. It still shows empty log sometimes. – Utkarsh Bhatt Mar 29 '18 at 07:10
  • 1
    Is your cloud function timing out still? – sketchthat Mar 30 '18 at 21:54
  • @yeah. It does. – Utkarsh Bhatt Mar 31 '18 at 16:56
  • 1
    Is this the only file you're uploading, or do you have other files. This file has no blank `console.log` requests, so it can't be outputting empty logs in firebase. – sketchthat Apr 02 '18 at 05:16
  • I made a temporary project to check out the token verification in Cloud functions. This is where I got those empty logs. The `index.js` is the only `js` file there along with the typical node stuff. – Utkarsh Bhatt Apr 02 '18 at 18:03
  • 1
    I'm unsure where else to look, my assumption is you have another file making the blank notifications in your file, because the code provided doesn't make any null calls. – sketchthat Apr 03 '18 at 03:49
  • It may be worth getting in touch with the Firebase support team directly with your issue: https://firebase.google.com/support/contact/troubleshooting/ – Ryan A. Apr 05 '18 at 11:39
  • 1
    @Utkarsh how about trying to increase your timeout period and see if that works. Go to your Google cloud console => click cloud functions => pick your cloud function => click edit => go further down probably click more => the default time out is 60 seconds, try increasing and see what happens. – Mr.O Aug 08 '18 at 09:31
  • @Mr.O. It's been quite a few months since I have seen empty logs. They don't show up anymore. Perhaps this was an issue with the cloud functions themselves. – Utkarsh Bhatt Aug 09 '18 at 10:10