Search code examples
pythonfirebasegoogle-cloud-platformfirebase-authentication

Firebase: maintain user lookup table in GCP


I created my first web app using Firebase and implemented user auth using FirebaseUI. In my Firebase console, I can see the Users table in the Authentication tab being created with a unique Identifier (phone number, e-mail). My Firebase project is also connected to my GCP project.

How can I see my Firebase app's table of users in GCP? Ideally, I would like to have that in a Datastore service or similar. Is there a way to sync users between Firebase and GCP? I need that, to allow my backend application in Cloud Run can do a quick lookup (based on the user's Identifier), and add more features to user's lookup table (e.g. edit preferences column).

So far, I could only find a function to check if the user exists in the Firebase's Users table directly:

from firebase_admin import auth, credentials

def is_user_in_firebase(identifier: str) -> bool:
    
    try:
        auth.get_user_by_phone_number(identifier)
    
    except auth.UserNotFoundError:
        return False
    
     else:
        return True

Solution

  • My Firebase project is also connected to my GCP project.

    Firstly, it's helpful to know that a Firebase project is a GCP project. They are not actually separate things. Firebase is just a set of configurations and services that are hosted in a Google Cloud project, and the Firebase console is an alternative way of viewing the stuff going on in that project (mostly just the Firebase-related services).

    How can I see my Firebase app's table of users in GCP?

    If you're asking how to see users registered with Firebase Authentication in the Google Cloud console, that's not possible. You must use the Firebase console to view Firebase Authentication data. There is no GCP-accessible "table" or database for that data.

    Ideally, I would like to have that in a Datastore service or similar.

    If you want user data to exist in a different GCP product, you will have to write some code to copy that as you see fit.

    I need that, to allow my backend application in Cloud Run can do a quick lookup (based on the user's Identifier)

    You can use the Firebase Admin SDK to manage Firebase Auth user data. You don't need to copy it to another data source.

    The function you're using now just queries users by phone number (if they used phone number authentication to sign up). If you want to query by user ID using Firebase Admin for python, you will want to start with the example code in the documentation for retrieving user data:

    from firebase_admin import auth
    
    user = auth.get_user(uid)
    print('Successfully fetched user data: {0}'.format(user.uid))