0
votes

According to the Firebase Functions Authentication Triggers documentation: "You can trigger Cloud Functions in response to the creation and deletion of Firebase user accounts."

Is it possible to trigger a Cloud Run service in the same way? I've also tried digging through the docs to see if Firebase Authentication will publish on a pub/sub topic, hoping I could trigger the Cloud Run service that way, but all I can find is documentation for Cloud Function triggers.

2

2 Answers

3
votes

For now, you can't trigger a Cloud Run on a Firebase event (and also on firestore event). Only Cloud Functions can be triggered. So, you can

  • Create a Cloud Functions that proxy the event (or as proposed by renaud, to transform this event in PubSub message that trigger your Cloud Run with a push subscription)
  • Create a Cloud Functions that perform the business logic that you wanted to do in Cloud Run
  • Wait for an update in Eventarc to take into account the firebase/firestore events (I haven't input on that could be in 2021 or not...)
3
votes

I've also tried digging through the docs to see if Firebase Authentication will publish on a pub/sub topic`

Yes it is totally possible for a Cloud Function to create and send messages to a specific topic. Let's see below an example for a user creation:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
// ...
const { PubSub } = require('@google-cloud/pubsub');
// ...

exports.publishToTopic = functions.auth.user().onCreate(async (user) => {
  // A Cloud Function triggered when a user is created
  // The below code should preferably be in a try/catch block
  
  const userId = user.uid;
  const email = user.email;

  const pubSubClient = new PubSub();

  const topic = 'firebase-user-creation';  // For example
  const pubSubPayload = { userId, email };
  const dataBuffer = Buffer.from(JSON.stringify(pubSubPayload));
  return pubSubClient.topic(topic).publish(dataBuffer);
  
});