触发云 运行 以响应 Firebase 身份验证事件

Trigger Cloud Run in response to Firebase Authentication event

根据 the Firebase Functions Authentication Triggers documentation:“您可以触发 Cloud Functions 以响应 Firebase 用户帐户的创建和删除。

是否可以用同样的方式触发云 运行 服务?我还尝试深入研究文档,看看 Firebase 身份验证是否会在 pub/sub 主题上发布,希望我能以这种方式触发 Cloud 运行 服务,但我只能找到 Cloud Function 的文档触发器。

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

是的,云函数完全有可能创建消息并将消息发送到特定主题。让我们看下面的用户创建示例:

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);
  
});

目前,您无法在 Firebase 事件(以及 firestore 事件)上触发 Cloud 运行。只能触发 Cloud Functions。所以,你可以

  • 创建代理事件的 Cloud Functions(或按照 renaud 的建议,在 PubSub 消息中转换此事件,从而触发您的 Cloud 运行 推送订阅)
  • 创建一个 Cloud Functions 来执行您想在 Cloud 中执行的业务逻辑 运行
  • 等待 Eventarc 中的更新以考虑 firebase/firestore 事件(我还没有输入可能在 2021 年或不...)