Firebase Authentication Triggers

Cloud Functions can be triggered by events from Firebase Authentication. These events include user creation and user deletion. For example, you could send a welcome email to a user who has just created an account in your app.

Event types

Firebase Authentication can trigger functions in response to user create and delete events.

Event Type Trigger
providers/firebase.auth/eventTypes/user.create Triggered when a user account is created.
providers/firebase.auth/eventTypes/user.delete Triggered when a user account is deleted.

User creation

Firebase accounts 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.

User deletion

You can also configure a function to trigger upon user deletion.

Event structure

Event data is provided as a userRecord object.

An example password-based account creation event is shown below:

{
  "email": "me@example.com",
  "metadata": {
      "createdAt": "2018-10-19T19:29:16Z"
  },
  "uid": "XXXXX"
}

Some properties of this object are only defined when using certain authentication methods. For example, password-based account events define an email property containing the user's email address. The uid property (which contains a user ID unique to your project) is always defined.

Sample code

Node.js 8/10

/**
 * Triggered by creation or deletion of a Firebase Auth user object.
 *
 * @param {object} data The event payload.
 * @param {object} context The event metadata.
 */
exports.helloAuth = (data, context) => {
  try {
    console.log(
      `Function triggered by creation or deletion of user: ${data.uid}`
    );
    console.log(`Created at: ${data.metadata.createdAt}`);

    if (data.email) {
      console.log(`Email: ${data.email}`);
    }
  } catch (err) {
    console.error(err);
  }
};

Node.js 6 (Deprecated)

/**
 * Triggered by a change to a Firebase Auth user object.
 *
 * @param {!Object} event The Cloud Functions event.
 */
exports.helloAuth = event => {
  try {
    const data = event.data;
    console.log(`Function triggered by change to user: ${data.uid}`);
    console.log(`Created at: ${data.metadata.createdAt}`);

    if (event.data.email) {
      console.log(`Email: ${data.email}`);
    }
  } catch (err) {
    console.error(err);
  }
};

Python

import json
def hello_auth(data, context):
    """ Triggered by creation or deletion of a Firebase Auth user object.
     Args:
            data (dict): The event payload.
            context (google.cloud.functions.Context): Metadata for the event.
    """
    print('Function triggered by creation/deletion of user: %s' % data["uid"])
    print('Created at: %s' % data["metadata"]["createdAt"])

    if 'email' in data:
        print('Email: %s' % data["email"])

Go

// Package firebase contains a Firestore Cloud Function.
package firebase

import (
	"context"
	"log"
	"time"
)

// AuthEvent is the payload of a Firestore Auth event.
type AuthEvent struct {
	Email    string `json:"email"`
	Metadata struct {
		CreatedAt time.Time `json:"createdAt"`
	} `json:"metadata"`
	UID string `json:"uid"`
}

// HelloAuth is triggered by Firestore Auth events.
func HelloAuth(ctx context.Context, e AuthEvent) error {
	log.Printf("Function triggered by creation or deletion of user: %q", e.UID)
	log.Printf("Created at: %v", e.Metadata.CreatedAt)
	if e.Email != "" {
		log.Printf("Email: %q", e.Email)
	}
	return nil
}

Deploying your function

To deploy your function, you need to specify the event type and the project for which you have Firebase Auth configured. In the GCP Console, there is a single field for Event Type as the project is assumed to be the same as the project that contains your function.

On the command line, however, you must use specific strings to specify these two parameters. The following gcloud command deploys a function that is triggered by user create events:

gcloud functions deploy YOUR_FUNCTION_NAME \
  --trigger-event providers/firebase.auth/eventTypes/user.create \
  --trigger-resource YOUR_PROJECT_ID \
  --runtime RUNTIME
Argument Description
--trigger-event NAME The name of the event type that triggers the function. In this case, it should be one of create or delete, as listed above.
--trigger-resource NAME The project ID (in this example, YOUR_PROJECT_ID) for the project that contains your function and Firebase Authentication.
--runtime RUNTIME The name of the runtime you are using. For a complete list, see the gcloud reference.
Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Functions Documentation