Somers Matthews
Somers Matthews

Reputation: 45

Google Firebase Function Firebase auth onCreate event handler - failed to configure trigger

I am trying to add a Firebase Authentication onCreate user event handler to insert user into Google Cloud SQL database. I got it to work successfully locally with the database public IP, and it successfully adds rows to the SQL database.

I ran firebase deploy -P gircapp2 and this is what I got from the google cloud function logs:


{
  "protoPayload": {
    "@type": "type.googleapis.com/google.cloud.audit.AuditLog",
    "status": {
      "code": 13,
      "message": "Failed to configure trigger providers/firebase.auth/eventTypes/[email protected] (__gcf__.us-central1.createGircUser)"
    },
    "authenticationInfo": {
      "principalEmail": "[email protected]"
    },
    "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"
}

Here is my index.js, which works locally and updates successfully google cloud SQL database:

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

})()
})

Upvotes: 0

Views: 630

Answers (1)

Donnald Cucharo
Donnald Cucharo

Reputation: 4126

It is possible that Firebase Auth is not yet enabled on your project because you are passing uid through a test environment to trigger the event instead of creating a new email on Firebase Console.

Fix it by going to Firebase Console > Firebase Authentication and set up Sign-In method. On your case, you can enable email or a federated identity provider (ex. Google):

enter image description here

Afterwards, redeploy your function then add user on the Firebase Console.


As an addition, when trying to connect to a Cloud SQL instance using Cloud Functions, you're supposed to connect with Unix Sockets by default. With your current code, you will encounter "Not Found" runtime error. You should fix it by changing your host to:

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

Upvotes: 1

Related Questions