Authentication triggers

Using Authentication triggers, we can execute function code in response to the creation and deletion of a user via Firebase Authentication.

To create a Cloud function that is executed if a new user is created, we can use the following code:

exports.userCreated = functions.auth.user().onCreate(event => { ... });

According to the Firebase documentation, user creation events for Cloud Functions occur in the following scenarios:

  • The developer creates an account using the Firebase Admin SDK
  • A user creates an email account and password
  • A user signs in for the first time using a federated identity provider
  • 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. If you would like to access attributes of a newly created user, you can do so using the event.data object.

For example, you can get the user's email and name as follows:

const user = event.data; 

const email = user.email;
const name = user.displayName;

Apart from user creation, if you want to trigger a function on user deletion, you can do it using the onDelete() event handler:

exports.deleteUser = functions.auth.user().onDelete(event => {
// ...
});
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset