2

I'm trying to set up a Firebase Auth trigger for user deletion using Firebase Functions v6.1.0, but I'm getting a TypeError. Here's my minimal reproduction case:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { getFirestore } = require('firebase-admin/firestore');
admin.initializeApp();

exports.registerUser = functions.https.onCall(async (data, context) => {
    
});

exports.deleteUser = functions.auth.user().onDelete(async () => { // <<<< ERROR
    
});

This produces the error:

TypeError: Cannot read properties of undefined (reading 'user')
    at Object.<anonymous> (/Users/thierry/Documents/firebase-app/functions/index.js:16:37)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
    at Module.load (node:internal/modules/cjs/loader:1317:32)
    at Module._load (node:internal/modules/cjs/loader:1127:12)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
    at Module.require (node:internal/modules/cjs/loader:1339:12)
    at require (node:internal/modules/helpers:135:16)
    at loadModule (/Users/thierry/Documents/firebase-app/functions/node_modules/firebase-functions/lib/runtime/loader.js:40:16)

Environment:

firebase-functions: 6.1.0 firebase-admin: 12.7.0 Node.js: 22

I've tried various syntaxes including:

functions.auth.user().beforeDelete functions.identity.beforeUserDeleted functions.auth.userDeleted().onEvent

But none of them work. According to the Firebase documentation, this should be the correct syntax. How can I properly set up an Auth trigger for user deletion in Firebase Functions v6.1.0?

Thanks!

2 Answers 2

3

As stated in the Auth triggers documentation, firebase functions Gen 2 do not have Auth triggers. They were replaced by identity triggers.
You have 2 options to address this:

  1. Import gen 1 functions as const functions = require('firebase-functions/v1');
  2. Use the beforeUserCreated trigger. See documentation for more details.
Sign up to request clarification or add additional context in comments.

Comments

0

As per the Firebase documentations on Trigger a function on user deletion it expects a user variable to be used within the callback function

Which is why you are getting undefined for user.

exports.deleteUser = functions.auth.user().onDelete(async (user) => {
  // ...                                                     
});

5 Comments

Unfortunately I am getting the same error: TypeError: Cannot read properties of undefined (reading 'user') at Object.<anonymous>
Using this minimal script: const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); exports.deleteUser = functions.auth.user().onDelete(async (user) => { });
You exemple is for gen 1st API right?
@thierryb Yes I'm using 1st gen API. Did you initialize it using firebase init functions or firebase init then choose JavaScript as a function language ? Right ? Could you also try the same by using typescript since these errors are better caught in typescript.
Also consider importing firebase-functions as firebase-functions/v1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.