Extend Firebase Authentication with Cloud Functions

You can trigger functions in response to the creation and deletion ofFirebase user accounts. For example, you could send a welcome email to auser who has just created an account in your app. Examples on this page arebased on a sample that does exactly this—sends welcome and farewell emailsupon account creation and deletion.

For more examples of use cases, seeWhat can I do withCloud Functions?.

Note:Cloud Functions for Firebase (2nd gen) does not provide support for theevents and triggers described in this guide. Because 1st gen and 2nd gen functions can coexist side-by-side in the same source file, you can stilldevelop and deploy this functionality together with 2nd gen functions.

Trigger a function on user creation

You can create a function that triggers when aFirebase user iscreated using thefunctions.auth.user().onCreate()event handler:

exports.sendWelcomeEmail=functions.auth.user().onCreate((user)=>{//...});

Firebase accounts will trigger user creation events forCloud Functions when:

  • A user creates an email account and password.
  • A user signs in for the first time using a federated identity provider.
  • The developer creates an account using theFirebase Admin SDK.
  • A user signs in to a new anonymous auth session for the first time.

ACloud Functions event isnot triggered when a user signs in for thefirst time using a custom token.

Access user attributes

From the user data returned to your function, you canaccess the list of user attributes available in the newly created user'sUserRecordobject. For example, you can get the user's email and display name as shown:

constemail=user.email;//Theemailoftheuser.constdisplayName=user.displayName;//Thedisplaynameoftheuser.

Trigger a function on user deletion

Just as you can trigger a function on user creation, you canrespond to user deletion events. Use thefunctions.auth.user().onDelete()event handler as shown:

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

Caution: Deleting multiple users at once using the Firebase Admin SDK (forexample,admin.auth().deleteUsers([uid1, uid2]) in Node.js) does not fireuser deletion events, so event handlers set up usingfunctions.auth.user().onDelete()will not be triggered. Delete users one ata time if you want user deletion events to fire for each deleted user.

Trigger blocking functions

If you've upgraded toFirebase Authentication with Identity Platform, you can extendFirebase Authentication usingblockingCloud Functions.

Blocking functions let you execute custom code that modifies the result of auser registering or signing in to your app. For example, you can prevent a userfrom authenticating if they don't meet certain criteria, or update a user'sinformation before returning it to your client app.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-10-01 UTC.