Extend Firebase Authentication with Cloud Functions

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

For more examples of use cases, see What can I do with Cloud Functions?.

Trigger a function on user creation

You can create a function that triggers when a Firebase user is created using the functions.auth.user().onCreate() event handler:

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

Firebase accounts will trigger user creation events for Cloud 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 the Firebase Admin SDK.
  • A user signs in to a new anonymous auth session for the first time.

A Cloud Functions event is not triggered when a user signs in for the first time using a custom token.

Access user attributes

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

const email = user.email; // The email of the user.
const displayName = user.displayName; // The display name of the user.

Trigger a function on user deletion

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

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

Trigger blocking functions

If you've upgraded to Firebase Authentication with Identity Platform, you can extend Firebase Authentication using blocking Cloud Functions.

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