【发布时间】: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