pythonfirebasegoogle-cloud-firestore

Firestore missing or insufficient permission firebase-rest-api Python


I'm getting this error only when the database rule is set to:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

and if I use this rule:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

This works, but there are too many security risks so I decided to use the first one but in my coding am I doing something wrong:

#initialization
app = firebase.initialize_app(firebaseConfig)
auth = app.auth()
db = app.firestore()

#signup method

def signUp():
    clear()
    print('Signup menu')
    print('Never forget your password or email; you will lose all your credits.\nIf this happens, contact furjack')
    email = input("Enter your email address: ")
    password = input("Enter your password: ")

    try:
        user_info = auth.create_user_with_email_and_password(email, password)
        user_id_token = user_info['idToken']

        user_wallet_ref = db.collection('users-wallet').document(email)
        user_wallet_ref.set({'wallet': 0})

        print("Successfully signed up")
        auth.send_email_verification(user_id_token)
        print('We have sent you a verification email. Please verify your email to use the app.')

        input('Press enter to continue...')

        logIn()
    except:
        print(f"Error during signup")
        input('Press enter to continue...')
        exit()

This is it basically where am I doing wrong or is Google wrong or what is wrong?


Solution

  • First you have to install:

    pip install firebase-admin
    

    After installing go to console and click on the project settings from the gear icon next to the project name.
    Now don't use Firestore from firebase-rest-api instead we're going to use from firebase_admin.
    Then click the service settings and press generate new key. Click it and it will tell u to download the json file. Download it next to your main.py ok after that open your main.py and import libraries like this:

    import firebase_admin
    from firebase_admin import credentials
    from firebase_admin import firestore
    

    After importing we're going to say the Firebase admin to use the credentials we got from the service section in console it's done like this:

    cred = credentials.Certificate('/path/to/jsonfile')
    firebase_admin.initialize_app(cred)
    
    db = firestore.client()
    

    It should be initialized like this and db will be the variable we're going to use.

    Now let's try an example:

    user_wallet_ref = db.collection('users-wallet').document('test@gmail.com')
    user_wallet_ref.set({'wallet': 0})
    

    When we test this time it works basically the problem was we have to use firebase_admin when we give permission to read write: if <condition>