Firebase chat app setValue failed error with a public database?
Your code is using the Firebase Realtime Database, but you're changing the security rules for Cloud Firestore. While both databases are part of Firebase, they are completely different and the server-side security rules for one, don't apply to the other.
When you go the database panel in the Firebase console, you most likely end up in the Cloud Firestore rules:
If you are on the Cloud Firestore rules in the Firebase console, you can change to the Realtime Database rules by clicking Cloud Firestore BETA at the top, and then selecting Realtime Database from the list.
You can also directly go to the security rules for the Realtime Database, by clicking this link.
The security rules for the realtime database that match what you have are:
{
"rules": {
".read": "auth.uid !== null",
".write": "auth.uid !== null"
}
}
This will grant any authenticated user full read and write access to the entire database. Read my answer to this question on more on the security/risk trade-off for such rules: Firebase email saying my realtime database has insecure rules.
Flutter with Firebase DatabaseError: Permission denied
Those are the rules for firestore:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;
while in your code you are using the realtime database. You need to navigate to the realtime database tab in the console and update the rules to the following:
{
// Allow read/write access to all users under any conditions
// Warning: **NEVER** use this ruleset in production; it allows
// anyone to overwrite your entire database.
"rules": {
".read": true
".write": true
}
}
Read more about it here:
https://firebase.google.com/docs/rules/insecure-rules#database
Firebase setvalue DatabaseException: Failed to parse node with class class
The completion listener on your call to setValue()
is from the legacy 2.x.x SDK: Firebase.CompletionListener(). You must use the completion listener from the new 9.x.x SDK, DatabaseReference.CompletionListener().
The two SDKs are not compatible. You should use the new SDK exclusively. Update your build.gradle
to remove:
compile 'com.firebase:firebase-client-android:2.x.x'
See the Upgrade Guide for more details.
failed: permission_denied when setValue: or removeValue: in Firebase Database | Swift 4
Firebase needs to be configured in your AppDelegate like this
func application(_ application: UIApplication, didFinishLaunchingWithOptions... {
FirebaseApp.configure()
Then in your viewController set up a class var:
class ViewController: UIViewController{
var ref: DatabaseReference!
so, don't do this: var ref = DatabaseReference.init()
Then in a viewDidLoad, initialize the class var and then authenticate
override func viewDidLoad() {
super.viewDidLoad()
self.ref = Database.database().reference()
//proceed to authenticate
Also, Firestore rules do NOT apply to the Firebase Realtime Database, it has its own set of rules.
service cloud.firestore //doesn't do anything to RTDB
So leave your Firebase database rules as the default of
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
test with firebase database throwing error
If you want to Mock the full call chain you could create mocks for the intermediate states like this (I don't know all the right types to use here for realtime database, but a similar approach works for Firestore)
val databaseMock = mock(DatabaseReference::class.java)
val childMock = mock(Reference::class.java)
val mockTask = mock(??) // set type to whatever "setValue" returns
doReturn(childMock).`when`(databaseMock).child(anyString())
doReturn(childMock).`when`(childMock).child(anyString())
doReturn(mockTask).`when`(childMock).setValue(any())
If you want to actually test that the correct value was set, you can add a listener to the mock to intercept the actual value passed to it
doAnswer { invocation ->
val args = invocation.arguments
val l = args[0] as Map<String,Any>
//add tests here to assert that the map values you sent are correct
null
}.`when`(childMock).setValue(any())
Debugging Tips
If you want to diagnose what is going on in a scenario like this you can change the chained call in your real code to something like the code below. Then if one of the calls returns null you will know exactly which one it is and can add the missing mock for it.
val db = ServiceLocator.reference
val cm = db.child("Messages")
val ct = cm.child(System.currentTimeMillis().toString())
val response = ct.setValue(messageMap)
response.await()
None of these call should access your database in the test, so the actual database schema does not matter. All that matters is getting the mocks set correctly (since you are using a mock database anyway)
Firebase Database Access Issue: Client doesn't have permission to access the desired data
Make sure you are selecting the Realtime Database
, it seems to me you are setting the rules on firestore but using the realtime database.
Your realtime database;s rules should look something like this:
{
"rules": {
".read": true,
".write": true
}
}
Related Topics
Get Free Space on Internal Memory
How to Set Font Custom Font to Spinner Text Programmatically
How to Display Icons in a Popupmenu
Android - Controlling a Task with Timer and Timertask
Detecting Sms Incoming and Outgoing
Date Formatting Based on User Locale on Android
Android - Timepicker Minutes to 15
Spinner Does Not Wrap Text -- Is This an Android Bug
Failed to Find Build Tools Revision 23.0.1
How to Debug Android Application Line by Line Using Eclipse
Rotating Image on a Canvas in Android
Put and Get String Array from Shared Preferences
How to Add Buttons at Top of Map Fragment API V2 Layout
Find Out If Listview Is Scrolled to the Bottom