# Customizing the authentication flow using asynchronous functions

This document shows you how to extend Identity Platform authentication
using asynchronous [Cloud Run functions](https://docs.cloud.google.com/functions).

Asynchronous functions let you trigger non-blocking tasks in response to user
creation and deletion. They are useful for starting long-running operations or
performing auxiliary tasks, like sending a welcome email.

To directly modify the result of an authentication operation, see
[Extending authentication with blocking functions](https://docs.cloud.google.com/identity-platform/docs/blocking-functions.md.txt).
The user object that an asynchronous function receives does not contain
updates from the blocking function.

## Before you begin

Create an app with Identity Platform. See the
[Quickstart](https://docs.cloud.google.com/identity-platform/docs/quickstart-email-password) to learn how.

## Creating an asynchronous function

To create and deploy an asynchronous function, follow the steps in
[Get started: write, test, and deploy your first functions](https://firebase.google.com/docs/functions/get-started).

## Responding to user creation

The `onCreate` event triggers whenever a user account is created. This includes
anonymous sessions and accounts created with the Admin SDK. The function does
not trigger when a user signs in for the first time using a custom token.

The following example shows how to register a handler for `onCreate`:

### Node.js

```javascript
exports.myFunction = functions.auth.user().onCreate((user) => {
  // TODO.
});
```

## Responding to user deletion

The `onDelete` event triggers whenever a user account is deleted. The following
example shows how to register a handler for `onDelete`:

### Node.js

```javascript
exports.myFunction = functions.auth.user().onDelete((user) => {
  // TODO.
});
```

## Getting user information

The `onCreate` and `onDelete` events provide `User` and `EventContext` objects
that contain information about the created or deleted user. For example:

### Node.js

```javascript
exports.myFunction = functions.auth.user().onCreate((user, context) => {
  const email = user.email; // The email of the user.
  const displayName = user.displayName; // The display name of the user.
});
```

See the
[`UserRecord` API reference](https://firebase.google.com/docs/reference/admin/node/firebase-admin.auth.userrecord)
and the
[`EventContext` API reference](https://firebase.google.com/docs/reference/functions/firebase-functions.eventcontext)
for a list of available fields.

## What's next

- Extend authentication with [blocking functions](https://docs.cloud.google.com/identity-platform/docs/blocking-functions.md.txt).
- Learn more about [Cloud Run functions](https://docs.cloud.google.com/functions/docs.md.txt).