0
votes

The Firebase admin SDK for Node.js provides us with a way of retrieving every user in our project, as seen in the documentation here.

I have implemented this in my own code as follows:

const listAllUsers = (nextPageToken) => {
  return new Promise((resolve, reject) => {
    // List batch of users, 1000 at a time.
    const customerUIDs = []
    admin.auth().listUsers(1000, nextPageToken)
      .then((listUsersResult) => {
        listUsersResult.users.forEach((userRecord) => {
          // check for customers by their claims
          if (userRecord.toJSON().customClaims.customer) {
            customerUIDs.push(userRecord.toJSON().uid)
          }
        })
        if (listUsersResult.pageToken) {
          // List next batch of users.
          listAllUsers(listUsersResult.pageToken)
        }
        resolve(customerUIDs)
      })
      .catch((error) => {
        reject(error)
      })
    })
}
...
// some
// more 
// code

NB: When a user is created, we assign the custom claims of customer: true to specify that the user is a customer, not an admin. That happens in my Cloud functions, so no need to paste it here again.

My question is this: This function above is a one-time operation. How do I listen for new users, and add them to the customerUIDs array?

1

1 Answers

0
votes

The Admin SDK doesn't provide any way to listen for newly created users.

If you want to write some code that runs when a new user account is created, you should look into using an Authentication trigger provided by Cloud Functions.