Android Firebase setValue() Permission Denied

…衆ロ難τιáo~ 提交于 2020-01-01 10:58:26

问题


This is how the rules are defined on firebase:

{
"rules": {
 "users": {
  ".read": true,
    "$user_id": {
     ".write": "auth.uid === $user_id",
     ".read" : true
  }}}}

I have succesfully written new user information in my register activity using setValue() just like below, and also added new comments using push().setValue(), each time I didn't have an issue with authentication. New user written straight to http://DB/users/ while comments were added very deep.

Here is a code snippit, my code hits the Log: Data could not be saved. Everytime.

AuthData authData = newRef.getAuth();

    if (authData != null) {
        Log.v(LOG_TAG, "Auth as " + authData.getUid());
        Map<String, Object> newEntry = new HashMap<String, Object>();
        newEntry.put("Name", name);
        newEntry.put("Description", desc);
        newEntry.put("DueDate", date);
        newEntry.put("Status", 0);

        newRef.setValue(newEntry, new Firebase.CompletionListener() {
            @Override
            public void onComplete(FirebaseError firebaseError, Firebase firebase) {
                if (firebaseError != null) {
                    Log.v(LOG_TAG, "Data could not be saved. " + firebaseError.getMessage()); //Hits here every time.
                } else {
                    Log.v(LOG_TAG, "Data saved successfully. Finishing activity...");
                    finish();
                }
            }
        });
        ;
    }
    else{
        Log.v(LOG_TAG, "No authdata");
    }

Hopefully I've provided enough information, I'm guessing the issue is in the security/rules but any direction/guidance would be very appreciated. Thank you.


回答1:


I was too stuck on this point and here's what helped me.

First things first, there are two types of users who can access database from firebase

  1. Authorized
  2. Non-authorized

By default it is set to non-authorized but then they do not have any permissions neither read nor write, so initially if you try to perform any operation you get the permission denied error.

How to change this to suit your requirement?

Solution:

Under Database section, go to Rules tab. The URL will be something like this with your project name.
https://console.firebase.google.com/project/project-name/database/rules

You will see somthing like this here:

{
  "rules": {
    ".read": "auth != null", //non-authorised users CANT read
    ".write": "auth != null" //non-authorised users CANT write
  }
}

Just change this to

{
  "rules": {
    ".read": "auth == null", //even non-authorised users CAN read
    ".write": "auth == null" //even non-authorised users CAN write
  }
}

Change this as per your requirement. Happy coding :)




回答2:


I had the wrong path in my DB Reference.

http://DB-PATH/simplelogin:00/PERMISSION-DENIED

http://DB-PATH/users/simplelogin:00/Can-only-add-here




回答3:


For people who have this problem, do not forget to change the "type of database" that by default gives us firebase, it should be noted that there is 2 type of database one that is Cloud Firestore and another that is RealTime Database

To answer the question, make sure you are using Realtime database:

Then edit the rules:

Obviously this configuration of rules should only be used for development environments, do not forget to change the rules when it is in production.



来源:https://stackoverflow.com/questions/29901935/android-firebase-setvalue-permission-denied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!