javascriptnode.jsfirebasegoogle-cloud-functions

Firebase Cloud Functions V2: A user creation trigger


I'm using cloud functions V2 with Node.js (not Typescript) and I need user creation trigger. In docs I've found something, but it doesn't work. By the link I also can see the following hint there:

Cloud Functions for Firebase (2nd gen) does not provide support for the events and triggers described in this guide.

Auth triggers doesn't work anymore for V2 cloud functions? If so, what is an alternative for this functionality? Below my code and deploy error:

const {functions} = require("firebase-functions");
initializeApp();
const db = getFirestore();

exports.authUserCreated = functions.auth.user().onCreate({region: "europe-west1"}, (user) => {
  const userEmail = user.email;
  const userId = user.uid;
  return db.collection("users").doc(userId).update({createdAt: FieldValue.serverTimestamp(), email: userEmail});
});

Deploy error: TypeError: Cannot read properties of undefined (reading 'auth')


Solution

  • Your require statement is wrong. It should be:

    const functions = require('firebase-functions');