【问题标题】:Google Firebase Function Firebase auth onCreate event handler - failed to configure triggerGoogle Firebase 功能 Firebase auth onCreate 事件处理程序 - 未能配置触发器
【发布时间】:2021-05-07 00:28:03
【问题描述】:

我正在尝试添加 Firebase 身份验证 onCreate 用户事件处理程序以将用户插入 Google Cloud SQL 数据库。我让它在本地使用数据库公共 IP 成功工作,并且成功地将行添加到 SQL 数据库。

我跑了firebase deploy -P gircapp2,这是我从谷歌云功能日志中得到的:


{
  "protoPayload": {
    "@type": "type.googleapis.com/google.cloud.audit.AuditLog",
    "status": {
      "code": 13,
      "message": "Failed to configure trigger providers/firebase.auth/eventTypes/user.create@firebaseauth.googleapis.com (__gcf__.us-central1.createGircUser)"
    },
    "authenticationInfo": {
      "principalEmail": "gircapptest@gmail.com"
    },
    "serviceName": "cloudfunctions.googleapis.com",
    "methodName": "google.cloud.functions.v1.CloudFunctionsService.UpdateFunction",
    "resourceName": "projects/gircapp2/locations/us-central1/functions/createGircUser"
  },
  "insertId": "46dje7cgk6",
  "resource": {
    "type": "cloud_function",
    "labels": {
      "function_name": "createGircUser",
      "region": "us-central1",
      "project_id": "gircapp2"
    }
  },
  "timestamp": "2021-02-02T19:01:54.537912Z",
  "severity": "ERROR",
  "logName": "projects/gircapp2/logs/cloudaudit.googleapis.com%2Factivity",
  "operation": {
    "id": "operations/Z2lyY2FwcDIvdXMtY2VudHJhbDEvY3JlYXRlR2lyY1VzZXIvQU82VHdMQkROTUE",
    "producer": "cloudfunctions.googleapis.com",
    "last": true
  },
  "receiveTimestamp": "2021-02-02T19:01:54.579884326Z"
}

这是我的 index.js,它在本地工作并成功更新谷歌云 SQL 数据库:

const functions = require("firebase-functions");
const { Sequelize, Model, DataTypes } = require('sequelize')
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager')

var password = ""

const name = 'projects/gircapp2/secrets/postgrespassword/versions/latest';

// Instantiates a client
const client = new SecretManagerServiceClient();

async function accessSecretVersion() {
  const [version] = await client.accessSecretVersion({
    name: name,
  });

  // Extract the payload as a string.
  const payload = version.payload.data.toString();

  password = payload
}

// run this in google functions shell emulator to make a user:
// firebase functions:shell
// firebase > createGircUser({uid: "654321"}) 

class User extends Model {}

exports.createGircUser = functions.auth.user().onCreate((firebaseuser) => {
(async () => {

    const user = 'postgres'
    const host = 'gircapp2:us-central1:gircpostgres'
    const database = 'postgres'
    const port = '5432'

    await accessSecretVersion();

    const sequelize = await new Sequelize(database, user, password, {
        host,
        port,
        dialect: 'postgres',
        logging: false
      })

    await User.init({
        userId: {
            type: DataTypes.STRING
        },
        name: {
            type: DataTypes.STRING
        },
        email: {
            type: DataTypes.STRING
        },
        speciality: {
            type: DataTypes.STRING
        },
        degree: {
            type: DataTypes.STRING
        },
        verified: {
            type: DataTypes.BOOLEAN
        }
    }, {
        sequelize,
        modelName: "user",
        timestamps: true,
    })

    const userId = firebaseuser.uid

    await User.sync({force: false}).then(() => {
        // users table created
        return User.create({
          userId: userId,
          verified: false,
        });
      });

})()
})

【问题讨论】:

    标签: firebase google-cloud-platform firebase-authentication google-cloud-functions


    【解决方案1】:

    您的项目可能尚未启用 Firebase 身份验证,因为您通过测试环境传递 uid 以触发事件,而不是在 Firebase 控制台上创建新电子邮件。

    通过转到 Firebase 控制台 > Firebase 身份验证并设置登录方法来修复它。根据您的情况,您可以启用电子邮件或联合身份提供商(例如 Google):

    然后,重新部署您的函数,然后在 Firebase 控制台上添加用户。


    另外,在尝试使用 Cloud Functions 连接到 Cloud SQL 实例时,您应该默认连接 with Unix Sockets。使用您当前的代码,您将遇到“未找到”运行时错误。您应该通过将host 更改为:

    const host = '/cloudsql/gircapp2:us-central1:gircpostgres'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 2020-10-20
      • 2018-08-21
      • 2019-06-19
      • 2020-11-02
      相关资源
      最近更新 更多