skip to Main Content

In my app Kotlin for chat, I have a small problem when I use addValueEventListener()to fetch data from users in my activity for a chat i retrieve all data in my consol firebase and my activity for chat it represents correctly profile of the user but when I siginup and reach profile user in RecyclerView i retrieve all user the problem when I click logout from menu all is ok but I receive this message "Error to get from database to MainActivity" from this fun override fun onCancelled(error: DatabaseError)

private fun updateUserListFromDatabase() {
    mDbRef.child("user").addValueEventListener(object : ValueEventListener {

        override fun onDataChange(snapshot: DataSnapshot) {
            userList.clear()

            val previousSize = userList.size

            for (postSnapshot in snapshot.children) {
                val currentUser = postSnapshot.getValue(User::class.java)

                if (mAuth.currentUser?.uid != currentUser?.uid) {
                    userList.add(currentUser!!)
                }
            }

            val currentSize = userList.size
            if (previousSize < currentSize) {
                userAdapter.notifyItemRangeInserted(previousSize, currentSize - previousSize)
            } else if (previousSize > currentSize) {
                userAdapter.notifyItemRangeRemoved(currentSize, previousSize - currentSize)
            } else {
                userAdapter.notifyItemRangeChanged(0, currentSize)
            }
        }

        override fun onCancelled(error: DatabaseError) {
            Toast.makeText(
                this@MainActivity,
                "Error to get from database to MainActivity",
                Toast.LENGTH_SHORT
            ).show()
        }
    })
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.menu, menu)
    return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if (item.itemId == R.id.logout) {
        mAuth.signOut()
        val intent=Intent(this@MainActivity,LogIn ::class.java)
        finish()
        startActivity(intent)

        return true
    }

    return true
}

I tried to logout from this activity where RecycleView for profile user without receiving this message from override fun onCancelled(error: DatabaseError):

Error to get from database to MainActivity

2

Answers


  1. Chosen as BEST ANSWER

    i want solution with sucure rules


  2. The following line of code:

    Toast.makeText( this@MainActivity, "${error.message}", Toast.LENGTH_SHORT).show()
    

    Produces this error:

    Client doesn’t have permission to access the desired data

    Because you’re using the following rules:

    {
       "rules":{
          ".read":"auth != null",
          ".write":"auth != null"
       }
    }
    

    When trying to read data under the user node while the user is not authenticated, the above error will always be thrown. So before trying to read something from the Realtime Database, always make sure that the user is authenticated by checking the FirebaseUser object against nullity:

    mAuth.currentUser?.let {
        //Read data from the Realtime Database.
    }
    

    So remember, the above rules won’t reject the read operation if the user is successfully authenticated in Firebase. However, for testing purposes, while developing the app, you can use the following rules:

    {
       "rules":{
          ".read":true,
          ".write":true
       }
    }
    

    But don’t forget to secure the database once your app gets into production.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search