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