Firebase Authentication Triggers

You can trigger a function in response to the creation and deletion of user accounts via Firebase Authentication. 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(event => {
  // ...
});

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.

The following account creation methods do not currently trigger Cloud Functions events: A user signs in for the first time using a custom token. A user signs in to a new anonymous auth session for the first time.

Access user attributes

Via event.data, 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 user = event.data; // The Firebase user.

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(event => {
  // ...
});

Send feedback about...

Need help? Visit our support page.