Rohit Kumar
Rohit Kumar

Reputation: 887

Filter User From Firebase AdminSDK

What I want to achieve here is, I should be able to pass phone numbers in an array and result should contain the user results matching with the phone number contained in the array.

Retrieving all the users are working just fine but I am not able to find out how to filter the results.

exports.listUsers = functions.https.onRequest(async(req, res) => {
    // List batch of users, 1000 at a time.
    const nextPageToken = req.query.token
    const mobileNumbers = req.query.phoneNumbers

    admin.auth().listUsers(1000, nextPageToken)
        .then(function(listUsersResult) {
            listUsersResult.users.forEach(function(userRecord) {
                console.log('user', userRecord.toJSON());
            });

            if (listUsersResult.pageToken) {
                // List next batch of users.
                listAllUsers(listUsersResult.pageToken);
            }

            return res.send(listUsersResult);
        })
        .catch(function(error) {
            console.log('Error listing users:', error);
        });
});

Upvotes: 2

Views: 1780

Answers (1)

Christopher Peisert
Christopher Peisert

Reputation: 24134

The Firebase Admin SDK provides APIs for retrieving individual users by phone number and by email. However, there is no batch retrieval API for filtering/querying all users by attributes such as phoneNumber or email.

Workaround 1: Retrieve all users and filter

  • Retrieve a list of all users
  • Filter the list

Workaround 2: Store users in another database

If filtering a large user base is a common operation for your app, then you can improve performance by storing users in a separate database.

  • When new users sign-up, store the user information in another database such as Firestore, MySQL, PostgreSQL.
  • Use standard query features to filter the results

Upvotes: 1

Related Questions