Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Firebase setValue() Permission Denied

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.

like image 580
thurst0n Avatar asked Apr 27 '15 17:04

thurst0n


3 Answers

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 :)

like image 106
Atul O Holic Avatar answered Oct 12 '22 07:10

Atul O Holic


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

like image 1
thurst0n Avatar answered Oct 12 '22 07:10

thurst0n


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:

Change the type of database

Then edit the rules:

Edit your security rules in the section of Database>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.

like image 1
Lucas Suarez Avatar answered Oct 12 '22 05:10

Lucas Suarez