List all users from Firebase Authentication

Question:

I am currently using the Pyrebase wrapper to get information (such as their email and created date) of all users. I tried looking through the documentation and cross reference it to Pyrebase documentation however i don’t seem to get what i’m looking for. Currently i have tried this:

import pyrebase

config={all required information, including path to service account .json file}
firebase=pyrebase.initialize_app(config)
db=firebase.database()
auth=firebase.auth()


extract_user = db.child('users').child('userId').get()

for x in extract_user.each():
    print(x.val())
    
    auth.get_account_info(user[x.val()])

However i still failed, i know im missing something but im not sure what.

Note: I saved the users userID in the database under userId. So i looped through each and every ID to be used in the ‘get_account_info’

Any suggestions or ways i can get this to be done?

Answers:

The db.child('users').child('userId').get() in your code reads users from the Realtime Database, where they’ll only exist if your application added the there explicitly. Adding a user to Firebase Authentication does not automatically also add it to the Realtime Database.

While Pyrebase allows you to initialize it with a service account, it doesn’t replicate all administrative functionality of the Firebase Admin SDKs. As far as I can see in Pyrebase’s code, Pyrebase’s does not implement a way to list users.

Consider using the Firebase Admin SDK, which has a built-in API to list users.

Answered By: Frank van Puffelen
from firebase_admin import credentials
from firebase_admin import auth

cred = credentials.Certificate("./key.json")
initialize_app(cred, {'databaseURL' : "your database..."})

page = auth.list_users()
while page:
for user in page.users:
        print("user: ", user.uid)
    page = page.get_next_page()

Then, after you get user id that looks like "F5aQ0kAe41eV2beoasfhaksfjh2323alskjal" you can see the actual email by:

user = auth.get_user("F5aQ0kAe41eV2beoasfhaksfjh2323alskjal")
print("user email: ", user.email)
Answered By: Yaki Hakimi