简体   繁体   中英

How do you list all users in firebase firestore function

I have been learning firebase firestore functions and I wanted to list all users and have then compare a value to that they have stored. Such as if there are 20 users and I want to see all users named "mike" how can I get an array of the users so I can compare them so I can find all users name "mike"?

I am running:

"firebase-admin": "^5.11.0",
"firebase-functions": "^1.0.0"

I saw this snippet of code for firebase admin but I dont thing it works as it wasn't labeled a firestore function, but if it does if I return the "admin.auth().listUsers..." will I get an arraylist of users?

    function listAllUsers(nextPageToken) {
  // List batch of users, 1000 at a time.
  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)
      }
    })
    .catch(function(error) {
      console.log("Error listing users:", error);
    });
}

Maybe you should do that search with the function getUserByDisplayName() . You can see it in this Firebase guide: https://firebase.google.com/docs/auth/admin/manage-users#list_all_users . But as Frank said that has nothing to do with firebase-functions. It is a part of Firebase Auth.

Hope this helps!

This is how I done it:

const functions = require('firebase-functions');
const admin = require('firebase-admin')

admin.initializeApp()

const express = require('express')
const app = express()

app.get('/users', async (req, res) => {
    try {
        const listUsers = await admin.auth().listUsers()

        return res.status(200).send({ listUsers })
    } catch (err) {
        return handleError(res, err)
    }
})

exports.api = functions.https.onRequest(app)

this shows a list of users but still need some work to make it better

Update

    exports.getAllUsers = async (req, res) => {
    var allUsers = [];
    return admin.auth().listUsers()
        .then(function (listUsersResult) {
            listUsersResult.users.forEach(function (userRecord) {
                // For each user
                var userData = userRecord.toJSON();
                allUsers.push(userData);
            });
            res.status(200).send(JSON.stringify(allUsers));
        })
        .catch(function (error) {
            console.log("Error listing users:", error);
            res.status(500).send(error);
        });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM