I have a list (uid's) of users that is connected to a group. On one page I want to list all usernames and emails of the users in the list.
What is the recommended way to fetch user data for each user? All I can find is in the admin sdk: admin.auth().getUser(uid)
admin.auth().getUsers([uid1, uid2, uid3])
?It's not possible to retrieve the list of all users with the client SDK, but you can do it in two ways:
Using the Firebase Realtime Database
You can add a node of user names and emails, say /users
in your database:
{
"users": {
"user1": {
"username": "user1",
"email": "user1@mail.com"
},
"user2": {
"username": "user2",
"email": "user2@mail.com"
}
}
}
Then just query this node using once('value')
:
const users = await firebase.database.ref('/userProfiles').once('value');
console.log(users);
Using a callable function and the Firebase Admin SDK
Create an HTTPS callable function, say getAllUsers()
, that retrieves all users. The Firebase Admin SDK has a way to list all users in batches of 1000.
Deploy the function using firebase deploy --only functions:getAllUsers
.
Then call the function from the client:
const getAllUsers = firebase.functions().httpsCallable('getAllUsers');
const users = await getAllUsers();
console.log(users);