Get All Users From Auth in Cloud Functions for Firebase

To get all the users from auth in Firebase, you can use the Admin SDK for Firebase.

It gives you elevated privileges to perform operations in the system that a normal user can not. The Admin SDK comes with a lot of functionality built-in. But, the one thing that we care about here is Managing Users.

Getting Users In Cloud Functions

Initialize a project in Firebase using the firebase-tools on your PC.

1
2
3
4
$ mkdir firebase-users && cd firebase-users
$ firebase init functions // follow the init instructions
$ cd functions
$ code . // opens the `/functions` folder in VSCode

In the index.js, write the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

const auth = admin.auth();

/**
* Gets all the users (1000 MAX) from Firebase auth.
*
* @param {Object} req Express Request Object.
* @param {Object} res Express Response Object
*/
const getAllUsers = (req, res) => {
const maxResults = 1; // optional arg.

auth.listUsers(maxResults).then((userRecords) => {
userRecords.users.forEach((user) => console.log(user.toJSON()));
res.end('Retrieved users list successfully.');
}).catch((error) => console.log(error));
};

module.exports = {
api: functions.https.onRequest(getAllUsers),
};

The auth.listUsers() is the function which returns a Promise with the userRecords object. It takes in two optional arguments.

  1. maxResults (optional): the number of users to get. Defaults to 1000.
  2. pageToken (optional): the uid of the user up to whom the results were fetched in the previous iteration of the function. This one is only useful if you are recursively calling the auth.listUsers() method to get more than 1000 users at a time from Firebase auth.

You can hit the endpoint using curl as follows:

1
curl http://localhost:5000/sweltering-fire-5301/us-central1/api

In your console, you will get the following response

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
uid: '877qtAdzX2MNwuZHHAwZkV1LNt82',
email: `[email protected]`,
emailVerified: true,
displayName: `John Doe`,
photoURL: `https://example/com/photo.png`,
phoneNumber: '+919090909090',
disabled: false,
metadata: {
lastSignInTime: 'Thu, 29 Mar 2018 07:29:41 GMT',
creationTime: 'Thu, 29 Mar 2018 07:29:41 GMT'
},
passwordHash: undefined,
passwordSalt: undefined,
customClaims: undefined,
tokensValidAfterTime: null,
providerData: [{
uid: '+919090909090',
displayName: undefined,
email: undefined,
photoURL: undefined,
providerId: 'phone',
phoneNumber: '+919090909090'
}]
}

This response is for a single user because the argument maxResults was set to 1. When you have multiple users in auth, you will get a larger JSON response.

Here, we are not sending the userRecords to the clients in the response. This is because that is probably sensitive data in most cases. Instead, we are using console.log() to print each userRecord in the console.

Of course, you can perform whatever operations inside the cloud function after receiving the userRecords from Firebase auth.