Firebase error: Permission denied. Unable to read/write from Firebase Database

I need to give read/write access only to authenticated users, but it seems like Firebase is not recognizing that the user is authenticated.

After I sign in the user with email and password, I am assuming user is authenticated and should be allowed to read/write from database. However permission is denied.

Here is the code for sign in activity:

 firebaseAuth.signInWithEmailAndPassword(email, password) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            //checking if success
            if(task.isSuccessful()){
                //display some message here
                // Toast.makeText(Register.this,"Successfully registered",Toast.LENGTH_LONG).show();


                sRef.authWithPassword(email,password,new Firebase.AuthResultHandler() {

                    @Override
                    public void onAuthenticated(AuthData authData) {
                        Toast.makeText(Register.this, user.getDisplayName()+" is authenticated",Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onAuthenticationError(FirebaseError firebaseError) {
                    }
                });

                Intent i = new Intent(Register.this,MainActivity.class);

                startActivity(i);

            }else{
                //display some message here
                Toast.makeText(Register.this, task.getException().getMessage(),Toast.LENGTH_LONG).show();
            }

        }
    });

I want to use the default rule in firebase that allows only auth users to read/write from database:

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

But when I change the rules as below my code is able to get read/write access to database.

{
"rules": {
".read": true,
".write":true
}
}

This is fine in testing, however I cannot continue with production with this method. Please help, how to authenticate a registered user to access database in Firebase.


Your code is a mixture of API calls from the legacy 2.xx SDK, for example:

sRef.authWithPassword(email,password,new Firebase.AuthResultHandler()

and the new 9.xx SDK:

firebaseAuth.signInWithEmailAndPassword()

The two SDKs are not compatible. You need to use or the other. The Upgrade Guide explains how to migrate from the legacy SDK to the new SDK.

You also need to use the Firebase console that corresponds to the SDK used: Legacy Console or new Firebase Console.

链接地址: http://www.djcxy.com/p/84220.html

上一篇: 如何在Firebase中仅允许读取和写入权限的用户?

下一篇: Firebase错误:权限被拒绝。 无法从Firebase数据库读取/写入