Permission denied in Firebase Realtime Database

64 Views Asked by At

I am trying to use pyrebase4 in my Kivy application and when attempting to access the information saved in the Realtime Database, the output in the terminal is:

[Errno 401 Client Error: Unauthorized for url: https://testdatabasept4-default-rtdb.firebaseio.com/users/QdYF1dur4wSpekS4YUETjrJBQfB3.json] {"error" : "Permission denied"}

My code:

firebaseConfig = ...
firebase=pyrebase.initialize_app(firebaseConfig)
auth=firebase.auth()

class LoginScreen(Screen):    
    dialog = None
    content = ObjectProperty()    

def do_login(self, email, senha):
        try:
            log_in = auth.sign_in_with_email_and_password(email,senha)
            user_log= auth.get_account_info(log_in['idToken'])
            email_verified = user_log['users'][0]['emailVerified']
            if email_verified:
                uid = log_in['localId']
                print(uid) # output: QdYF1dur4wSpekS4YUETjrJBQfB3
                db = firebase.database()
                user_info = db.child("users").child(uid).get()
                user_data = user_info.val()
                global nome
                nome=user_data['nome']
                global idEmail
                idEmail = email
                self.manager.current = 'authprojeto'
            else:
                auth.send_email_verification(log_in['idToken'])
                error_verification = MDDialog(
                    title='Erro',
                    text='O email ainda não foi verificado.\nFaça a verificação antes de fazer login.',
                    elevation=0,
                    size_hint=(0.8, 0.3),
                    buttons=[MDFlatButton(text='OK', on_release=lambda x: error_verification.dismiss())])
                error_verification.open()
        except Exception as e:
            print (e)
            error_validation = MDDialog(
                title='Erro no Login',
                text='Email ou senha inválidos.\nTente Novamente.',
                elevation=0,
                size_hint=(0.8, 0.3),
                buttons=[MDFlatButton(text='OK', on_release=lambda x: error_validation.dismiss())])
            error_validation.open()

...

My rules:

{
  "rules": {
    "users": {
      ".read": "auth != null",
      ".write": "auth != null",
    }
  }
}

What is wrong with my code or rule?

I tried to verify if the user is really authenticated with current_user, and apparently they are since it did not return None. If I set ".read": "auth != null" to True, it works, but I don't want non-authenticated users to have access.

0

There are 0 best solutions below