Android Firebase setValue () Permission denied
This is how rules are defined in Firebase:
{
"rules": {
"users": {
".read": true,
"$user_id": {
".write": "auth.uid === $user_id",
".read" : true
}}}}
I have successfully written new user information in my registry activity using setValue () as shown below and also added new comments using push (). SetValue (), every time I have no problem with authentication. The new user wrote directly to http: // DB / users / and the comments were added very deep.
Here is a code snippet, my code is logged: data cannot 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 security / regulations, but any direction / guidance would be much appreciated. Thank.
source to share
I was too stuck on this issue and here is what helped me.
First of all, there are two types of users that can access the database from firebase
- Authorized
- Unauthorized
By default it is configured for unauthorized access, but then they have neither read nor write permissions , so initially if you try to perform any operation you get permission denied.
How to change this according to your requirements?
Decision:
In the "Database" section, go to the "Rules" tab. The url will be like the name of your project.
https://console.firebase.google.com/project/project-name/database/rules
You will see something 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 :)
source to share
I had the wrong path in my DB link.
http: // DB-PATH / simplelogin: 00 / PERMISSION-DENIED
http: // DB-PATH / users / simplelogin: 00 / Can-only-add-here
source to share
For people who have this problem, remember to change the "database type" that the database gives us by default, it should be noted that there are 2 types of database: Cloud Firestore and the other is RealTime Database.
To answer the question, make sure you are using a live database:
Then edit the rules:
Obviously, this rule configuration should only be used for development environments, remember to change the rules when it is in production.
source to share