【问题标题】:Trigger Cloud Run in response to Firebase Authentication event触发 Cloud Run 以响应 Firebase 身份验证事件
【发布时间】:2021-08-01 16:49:32
【问题描述】:

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

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

【问题讨论】:

    标签: firebase firebase-authentication google-cloud-functions google-cloud-pubsub google-cloud-run


    【解决方案1】:

    我也尝试过浏览文档以查看 Firebase 身份验证将在发布/订阅主题上发布`

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

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

    【讨论】:

    • 当我尝试这个时,运行 .publish(dataBuffer) 时会超时。知道是什么原因造成的吗?
    • 是否需要设置任何特定的环境变量才能使其工作?
    • @ut9081 如果你添加一个 catch 块你会得到什么!
    • 它永远不会触发 catch 块,我只是得到:⚠ 函数:您的函数在 ~60 秒后超时。要配置此超时,请参阅firebase.google.com/docs/functions/…
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-10
      • 2016-11-27
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 2020-04-23
      • 2019-09-27
      相关资源
      最近更新 更多