Home > Software engineering >  Firestore permission denied without auth
Firestore permission denied without auth

Time:01-22

So I'm new to Firestore and I created a database, I added an android app, followed the steps ... But when I want to query the base I always get a "Permission denied" warning" even tho I changed the security rules of the database to allow every user to read and write on it. I just wanna use as a dummy for now without having to authenticate, is that possible

main.dart:

import 'package:chat_app/screens/chat_screen.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ChatScreen(),
    );
  }
}

other_screen:

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';

class ChatScreen extends StatelessWidget {
  const ChatScreen({super.key});


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('chats')
            .snapshots(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return CircularProgressIndicator();
          }
          final documents = snapshot.data!.docs;
          return ListView.builder(
            itemCount: documents.length,
            itemBuilder: (context, index) => Container(
              padding: EdgeInsets.all(8),
              child: Text(documents[index]['text']),
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        child: Icon(Icons.add),
      ),
    );
  }
}

Even after changing the security rules I still get the error

CodePudding user response:

The "Permission denied" warning you're getting is likely caused by the security rules you've set in your Firestore database. Based on the code you've provided, you're trying to read from a collection called "chats" in your Firestore database, but the security rules you've set don't allow that.

You can try the following steps:

  1. Go to the Firebase console, click on the Firestore tab, and then click on the "Rules" tab.

2.Make sure the rules for your database are set to allow read and write access for all users, like this:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

3.Save the rules and wait for them to propagate. This can take a few minutes. Also make sure that you have added the correct google-services.json file to your android project and also the correct google-services.plist file to your ios project. And make sure that you are calling the Firebase.initializeApp() before trying to access the database.

If you still get the error, please make sure that the Firestore is enabled in the Firebase console for your project.

CodePudding user response:

Here is all the debug console:

    Launching lib\main.dart on Galaxy S10 in debug mode...
√  Built build\app\outputs\flutter-apk\app-debug.apk.
Connecting to VM Service at ws://127.0.0.1:63666/9sb8aA6VBxc=/ws
W/DynamiteModule( 3746): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule( 3746): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller( 3746): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.
I/xample.chat_ap( 3746): The ClassLoaderContext is a special shared library.
I/xample.chat_ap( 3746): The ClassLoaderContext is a special shared library.
I/xample.chat_ap( 3746): The ClassLoaderContext is a special shared library.
W/xample.chat_ap( 3746): Accessing hidden field Ljava/nio/Buffer;->address:J (light greylist, reflection)
V/NativeCrypto( 3746): Registering com/google/android/gms/org/conscrypt/NativeCrypto's 295 native methods...
W/xample.chat_ap( 3746): Accessing hidden method Ljava/security/spec/ECParameterSpec;->getCurveName()Ljava/lang/String; (light greylist, reflection)
D/NetworkSecurityConfig( 3746): No Network Security Config specified, using platform default
I/ProviderInstaller( 3746): Installed default security provider GmsCore_OpenSSL
W/xample.chat_ap( 3746): Accessing hidden field Ljava/net/Socket;->impl:Ljava/net/SocketImpl; (light greylist, reflection)
W/xample.chat_ap( 3746): Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (light greylist, linking)
W/xample.chat_ap( 3746): Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (light greylist, linking)
W/xample.chat_ap( 3746): Accessing hidden method Ljava/security/spec/ECParameterSpec;->setCurveName(Ljava/lang/String;)V (light greylist, reflection)
W/xample.chat_ap( 3746): Accessing hidden method Ldalvik/system/BlockGuard;->getThreadPolicy()Ldalvik/system/BlockGuard$Policy; (light greylist, linking)
W/xample.chat_ap( 3746): Accessing hidden method Ldalvik/system/BlockGuard$Policy;->onNetwork()V (light greylist, linking)
W/Firestore( 3746): (24.4.0) [WatchStream]: (f57149) Stream closed with status: Status{code=PERMISSION_DENIED, description=Permission denied on resource project chat-app-ac2a2., cause=null}.
W/Firestore( 3746): (24.4.0) [OnlineStateTracker]: Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: Status{code=PERMISSION_DENIED, description=Permission denied on resource project chat-app-ac2a2., cause=null}
W/Firestore( 3746): This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
  • Related