0

I followed the API reference for organizing functions in one index.ts file, (https://firebase.google.com/docs/functions/organize-functions) but when checking the logs in the firebase console context is always null.

functions/src/index.ts

import * as functions from 'firebase-functions';
import { myFunction } from './myFireBaseFunctions'

const myFunction = functions.https.onCall(myFunctionHandler);

export default myFunction

functions/src/myFireBaseFunctions.ts

import * as functions from 'firebase-functions';

export const myFunction = async (context: functions.https.CallableContext) => {
  functions.logger.log('context', context);  // <-- null
  functions.logger.log('context.app', context.app);
  functions.logger.log('context.auth', context.auth);
  if (
    !context ||
    !context.app ||
    !context.auth ||
    !context.auth.token?.uid ||
    context.auth.token?.firebase?.sign_in_provider === 'anonymous' ||
    context.auth.token?.firebase?.email_verified === false
  ) {
    throw new functions.https.HttpsError(
      'unauthenticated',
      'must be called while authenticated.'
      );
    }
  }

  throw new functions.https.HttpsError(
    'unauthenticated',
    'must be called with different rights'
  );
};

// ...

when I call the function from my web app:

@services/firebaseFunctions.service.ts

import { Functions, httpsCallable } from 'firebase/functions';

export const myFunction = ( functions: Functions ) => {
  const firebaseFunction = httpsCallable(functions, 'myFunction');
  return firebaseFunction();
};

// ...

myComponent.tsx

import React from 'react'
import { useFunctions } from 'reactfire';
import { myFunction } from '@services/firebaseFunctions.service';

const MyComponent = () => {
  const functions = useFunctions();

  useEffect(() => {
    const myFunctionCall = async () => {
      const res = await myFunction(functions);
      console.log('res.data', res.data);
    };
    myFunctionCall();
  }, []);

  return <div>MyComponent</>
}

export default MyComponent
Erik Metz
  • 152
  • 1
  • 4

1 Answers1

1

The failure was in functions/src/myFireBaseFunctions.ts data needs to be specified.

export const myFunction = async (data:any, context: functions.https.CallableContext) => { // ... }

functions/src/myFireBaseFunctions.ts

import * as functions from 'firebase-functions';

export const myFunction = async (data:any, context: functions.https.CallableContext) => {
  functions.logger.log('context', context);  // <-- working now
  functions.logger.log('context.app', context.app);
  functions.logger.log('context.auth', context.auth);
  if (
    !context ||
    !context.app ||
    !context.auth ||
    !context.auth.token?.uid ||
    context.auth.token?.firebase?.sign_in_provider === 'anonymous' ||
    context.auth.token?.firebase?.email_verified === false
  ) {
    throw new functions.https.HttpsError(
      'unauthenticated',
      'must be called while authenticated.'
      );
    }
  }

  throw new functions.https.HttpsError(
    'unauthenticated',
    'must be called with different rights'
  );
};

// ...
Erik Metz
  • 152
  • 1
  • 4