2

I have tried a lots of things to solve this error but I'm unable to solve this error So if any one can help me to solve this error that will be great help

vehicleList.kt

@Suppress("DEPRECATION")
class VehiclesList : AppCompatActivity() {
    private val db = Firebase.firestore
    private lateinit var adapter: AdapterVehiclesList
    private var allVehicles: List<Vehicle> = ArrayList() // Store all vehicles from Firebase
    private lateinit var sharedPreferences: SharedPreferences
    private var userID:String = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_vehicles_list)
        val rv:RecyclerView = findViewById(R.id.rvVehicleList)
        val layoutManager = LinearLayoutManager(this)

        sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE)
        userID = sharedPreferences.getString("storedUserId", "userID").toString()

        FirebaseApp.initializeApp(this)
        val firebaseAppCheck = FirebaseAppCheck.getInstance()
        firebaseAppCheck.installAppCheckProviderFactory(SafetyNetAppCheckProviderFactory.getInstance())

        adapter = AdapterVehiclesList(ArrayList()) // Pass an empty list for now
        rv.layoutManager = layoutManager
        rv.adapter = adapter

        val searchView: EditText = findViewById(R.id.searchVehicle) // Replace with your SearchView ID
        searchView.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                filterData(s.toString().trim())
            }
            override fun afterTextChanged(s: Editable?) {}
        })
        fetchVehicleData(adapter)
    }

    private fun fetchVehicleData(adapter: AdapterVehiclesList) {
        val user = Firebase.auth.currentUser
        if (user != null) {
            val appCheck = Firebase.appCheck
            appCheck.getToken(true).addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    db.collection("Profiles").document(userID).collection("Vehicles")
                        .get()
                        .addOnSuccessListener { result ->
                            val vehicleList = ArrayList<Vehicle>()
                            for (document in result) {
                                val vehicleNumber = document.id
                                val driverName = document.getString("DriverName") ?: ""
                                val mobileNumber = document.getString("MobileNumber") ?: ""
                                val totalFilling = document.getDouble("TotalFilling") ?: 0.0

                                val vehicle =
                                    Vehicle(vehicleNumber, driverName, mobileNumber, totalFilling)
                                vehicleList.add(vehicle)
                            }
                            allVehicles = vehicleList // Store all vehicles
                            adapter.setData(vehicleList)
                        }
                        .addOnFailureListener { exception ->
                            Log.e("FetchData", "Error getting documents: ", exception)
                        }
                }
                else{
                    Toast.makeText(
                        this,
                        "Failed to Add vehicle\n Try Again...",
                        Toast.LENGTH_LONG
                    ).show()
                }
            }.addOnFailureListener{Exception->
                Log.e("AddVehicle", "Failed to Add vehicle2", Exception)
            }
        }
    }

My rules in Firestore:

rules_version = '2';

service cloud.firestore {
  match /databases/{database}/documents {
    match /Profiles/{userId} {
      allow read, write: if
        request.auth != null &&
        request.auth.token.firebase.identities['firestore.googleapis.com'] != null &&
        request.auth.token.firebase.identities['firestore.googleapis.com'] != [] &&
        request.auth.token.appCheck.claims['https://firebaseappcheck.googleapis.com/claims'] != null &&
        request.auth.uid == userId;
    }
    match /Profiles/{userId}/Vehicles/{vehicleId} {
      allow read: if
        request.auth != null &&
        request.auth.token.firebase.identities['firestore.googleapis.com'] != null &&
        request.auth.token.firebase.identities['firestore.googleapis.com'] != [] &&
        request.auth.token.appCheck.claims['https://firebaseappcheck.googleapis.com/claims'] != null &&
        request.auth.uid == userId;
    }
    match /{document=**} {
      allow read, write: if
        request.auth != null &&
        request.auth.token.firebase.identities['firestore.googleapis.com'] != null &&
        request.auth.token.firebase.identities['firestore.googleapis.com'] != [] &&
        request.auth.token.appCheck.claims['https://firebaseappcheck.googleapis.com/claims'] != null;
    }
  }
}

And error: Error getting documents: **

com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.

**

I have tried to solve this error by changing rules of firestore but I'm not able to solve this question I'm expecting that this function fetches all vehicles detail from firestore

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Does this answer your question? [firestore: PERMISSION\_DENIED: Missing or insufficient permissions](https://stackoverflow.com/questions/46590155/firestore-permission-denied-missing-or-insufficient-permissions) – Ivo Aug 15 '23 at 09:56
  • No, We have almost same problem and I have tried almost all suggestions from that question but still it's not working for me – Jaivik Kotadiya Aug 15 '23 at 11:51
  • 1
    Why are you checking for an app Check claim in your security rules instead of enforcing App Check in the console? I don't believe `request.auth.token.appCheck.claims` is a valid security rule (https://firebase.google.com/docs/reference/rules/index-all) – Alexander N. Aug 15 '23 at 15:56
  • 1
    Engineering just confirmed what Firebase team mate @AlexanderN. also said: security rules only execute once App Check allows the request, and there's no way to perform fine-grained checks on the App Check token in security rules. It's a fine feature request though, so I recommend [filing a feature request](https://firebase.google.com/support/troubleshooter/report) or posting it on firebase.uservoice.com. – Frank van Puffelen Aug 15 '23 at 16:24

1 Answers1

1

firebaser here

Security rules only execute once App Check allows the request, and there's no way to perform fine-grained checks on the App Check token in security rules.

It's a valid feature request though, so I recommend filing it with Firebase support or on firebase.uservoice.com.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807