Pavan
Pavan

Reputation: 139

Cloud Functions for Firebase running into infinite loop

I have a custom logic to verify the users.

I have written a Cloud Function for Firebase and to verify the custom tokens.

The problem is the cloud function is not getting terminated and is being run into infinite loop, till Firebase kills the function

The cloud function runs into infinite in both matching and non-matching scenario.

Below is the code:

/*  CLOUD FUNCTION */
exports.verifyToken = functions.https.onRequest((req, res) => {

  var corsFn = cors();
  corsFn(req, res, function () {
    verifyTheUserToken(req, res);

  });
});


function verifyTheUserToken(req, res) {


  if (!req.headers.authorization || !req.headers.authorization.startsWith('Bearer ')) {
    console.error('No Firebase ID token was passed as a Bearer token in the Authorization header.');
    res.status(403).send('Unauthorized');
  }

  const firebaseToken = req.headers.authorization.split('Bearer ')[1];
  const userId = req.body.uid;
  const receievedToken = req.body.token;

  return admin.auth().verifyIdToken(firebaseToken).then(decodedFirebaseToken => {
    console.log('ID Token correctly decoded', decodedFirebaseToken);
    console.log('req', req.body);
    return 'sucess';

  }).then(function (receivedValues) {

    return admin.database().ref().child('userTokens').child(userId).child('token').once('value');

  }).then(function (snapshot) {

    if (!snapshot.val()) {
      return Promise.reject('token is not set ');
    }

    if (snapshot.val() != receievedToken) {
      return Promise.reject('token doesnt match');
    }

    return 'verified';

  }).then(function (success) {

    return admin.database().ref().child('users').child(userId).child('isVerified').set(true);

  }).then(function (success) {

    console.log('The user is verified');
    return;

  }).catch(function (error) {

    console.log('Error', error);
    return;
  });

}

Client side I am doing a HTTP request to call the firebase cloud function.

/* CLIENT SIDE */
var currentUser = firebase.auth().currentUser.uid;
var firebaseUserToken = firebase.auth().currentUser.getToken();

firebase.auth().currentUser.getToken(/* forceRefresh */ true).then(function (firebaseUserToken) {
  fetch('https://us-central1-MYAPP.cloudfunctions.net/verifyToken', {

    'method': 'POST',
    'headers': {
      'Authorization': 'Bearer ' + firebaseUserToken,
      'Content-Type': 'application/json'
    },
    'body': JSON.stringify({
      'uid': currentUser,
      'token': 1234,
    })
  }).then(function (response) {
    console.log('successful response');
  }).catch(function (error) {
    console.error('Error in fetch', error);

  });

}).catch(function (error) {
  console.error('Error in getting firebase token', error);

});

I am unable to figure out the reason for the infinite loop.

I would really appreciate any help on this. Thanks!

Upvotes: 1

Views: 1122

Answers (1)

Pavan
Pavan

Reputation: 139

I had missed res.send() for the success case.

As per documentation:

Always end an HTTP function with send(), redirect(), or end(). Otherwise, your function might to continue to run and be forcibly terminated by the system.

https://firebase.google.com/docs/functions/http-events

Upvotes: 2

Related Questions