> Documentation index: https://firebase.google.com/docs/functions/llms.txt

> [!NOTE]
> **Note:** The 1st-gen functionality described in this guide is also supported in Cloud Functions (2nd gen) with improved features and performance. For more information about 2nd gen, see the [version comparison](https://firebase.google.com/docs/functions/version-comparison.md.txt). To go directly to the 2nd gen guide for this feature, see [Firebase Authentication triggers](https://firebase.google.com/docs/functions/auth-events.md.txt).

You can trigger functions in response to the creation and deletion of
Firebase Authentication 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?](https://firebase.google.com/docs/functions/use-cases.md.txt).

## Trigger a function on user creation

You can create a function that triggers when an Authentication user is created using
the
[`functions.auth.user().onCreate()`](https://firebase.google.com/docs/reference/functions/firebase-functions.auth.userbuilder.md.txt#authuserbuilderoncreate)
event handler:

<br />

```
exports.sendWelcomeEmail = functions.runWith({secrets: [emailApiKey]}).auth.user().onCreate((user) => {
  // ...
});
```

<br />

Authentication 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 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`](https://firebase.google.com/docs/reference/functions/firebase-functions.auth.md.txt#authuserrecord)
object. For example, you can get the user's email and display name as shown:

<br />

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

<br />

## 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()`](https://firebase.google.com/docs/reference/functions/firebase-functions.auth.userbuilder.md.txt#authuserbuilderondelete)
event handler as shown:

<br />

```
exports.sendByeEmail = functions.runWith({secrets: [emailApiKey]}).auth.user().onDelete((user) => {
  // ...
});
```

<br />

> [!CAUTION]
> **Caution:** Deleting multiple users at once using the Firebase Admin SDK (for example, `admin.auth().deleteUsers([uid1, uid2])` in Node.js) doesn't fire user deletion events, so event handlers set up using `functions.auth.user().onDelete()` *will not be triggered*. Delete users one at a time if you want user deletion events to fire for each deleted user.

## Trigger blocking functions

If you've upgraded to Firebase Authentication with Identity Platform, you can extend Firebase Authentication using
[blocking Cloud Functions](https://firebase.google.com/docs/auth/extend-with-blocking-functions.md.txt).

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.