Search code examples
firebasegoogle-cloud-functionsgcloudgcloud-cli

My Google Cloud Function Deploy CLI script is failing when deploying a Firebase Auth Create Trigger


Can someone please explain how I can deploy this firebase auth cloud function trigger with gcloud cli? I want to maintain my cloud functions with gcloud cli, not firebase cli.

package.json

{
  "name": "createUser",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "deploy": "gcloud functions deploy $npm_package_name --memory 128Mi --region us-central1 --allow-unauthenticated  --entry-point $npm_package_name --trigger-event-filters=type=user.create --trigger-event-filters=provider=firebase.auth --source . --runtime nodejs20 --gen2 --service-account realtime-missions-api@realtime-missions.iam.gserviceaccount.com --stage-bucket realtime-missions-functions "
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@google-cloud/functions-framework": "^3.3.0",
    "geofire-common": "^6.0.0",
    "firebase-admin": "^11.11.0"
  }
}

index.js

export const createUser = functions.auth.user().onCreate(async (user) => {
  try {
    const db = await getFirestore(app, 'realtime-missions-db');
    const docRef = await db.collection('userProfile').doc(user.uid);

    await docRef.set({
      email: user.email, 
      userID: user.uid
    })
    res.status(200).send('event started') 
  }
  catch(error){
    res.status(500).send(error)
  }
});

deployment script npm run deploy

I have tried changing the deployment event-trigger to the full type name when listing the triggers on gcloud cli with

--trigger-event-filters=type=providers/firebase.auth/eventTypes/user.create

--trigger-event providers/firebase.auth/eventTypes/user.create

The Event trigger type is listed here when listing the trigger types in gcloud. gcloud functions event-types list

which results in

providers/cloud.storage/eventTypes/object.change  
providers/firebase.auth/eventTypes/user.create   
providers/firebase.auth/eventTypes/user.delete    
providers/google.firebase.analytics/eventTypes/event.log  
providers/google.firebase.database/eventTypes/ref.create  
providers/google.firebase.database/eventTypes/ref.delete 
providers/google.firebase.database/eventTypes/ref.update 
providers/google.firebase.database/eventTypes/ref.write 
google.firebase.remoteconfig.update               
providers/cloud.firestore/eventTypes/document.create 
providers/cloud.firestore/eventTypes/document.delete  
providers/cloud.firestore/eventTypes/document.update 
providers/cloud.firestore/eventTypes/document.write

Solution

  • It's not supported. If you're using the Firebase Functions SDK to author a Cloud Function (which you are apparently doing), then you must absolutely use the Firebase CLI to deploy it. gcloud simply does not know how to deal with that code and configuration. The Firebase CLI is taking a special interpretation of the code it encounters and is able to translate that into a new set of code and configuration understood by Google Cloud. Similarly, the Firebase CLI can't deploy code that would normally only be understood by gcloud.

    According to the documentation:

    Cloud Functions for Firebase is optimized for Firebase developers:

    • Firebase SDK to configure your functions through code
    • Integrated with Firebase Console and Firebase CLI
    • The same triggers as Google Cloud Functions, plus Firebase Realtime Database, Firebase Authentication, and Firebase Analytics triggers