Search code examples
pythonfirebase-authenticationfirebase-admin

Extract Created date and last login from firebase authentication using Python


Currently my python code gets the user id and email of all users from firebase authentication using the firebase admin SDK, however I am unable find the correct syntax to extract the user metadata such as the created and last login date/time (which according to the documentation is in milliseconds). There are a few documentation that shows ways to do this, but it is not working for me.

My code:

from firebase_admin import credentials
from firebase_admin import auth

cred=credentials.Certificate('firebasesdk.json')
firebase_admin.initialize_app(cred, {
    "databaseURL": "myurl",
})

page = auth.list_users()
    while page:
        for user in page.users:
            print('User email: ' + user.email)
            print('User id: ' + user.uid)

I tried using user.metadata.creationTimeand user.madata.getLastSignInTimestamp and but it does not work with python. I was looking through this post regarding this issue.


Solution

  • It looks like the ExportedUsers result for list_users does not include the metadata for the users. In that case you'll need to call get_user(...) for each UID in the result to get the full UserRecord, and then find the timestamps in the metadata.