"FirebaseError: false for 'get' @ L5" firebase error when using on snapshot or get doc when using firebase emulator

Viewed 856

I am using firestore and firebase auth and I am trying to add custom usernames to my app (I am following this courses, Next.js Firebase Full Course) that connect to the freebase emulator because it is still in development. I have this piece of code that runs in a useEffect that gets the current user uid and then fetch a document from firestore, then checks if it exits and if i does set the username state to the username in the do. Here is the code:

    useEffect(() => {
        let unsubscribe;

        if (user) {
            console.log(user.uid);
            const ref = doc(db, "users", user.uid);
            unsubscribe = onSnapshot(ref, (doc) => {
                if (doc.exists()) {
                    setUsername(doc.data()?.username);
                } else {
                    console.log("No such document!");
                }
            });
        } else {
            setUsername(null);
        }

        return unsubscribe;
    }, [user]);

But I always get the error "next-dev.js?3515:32 Uncaught Error in snapshot listener: FirebaseError: false for 'get' @ L5". I have tried changing the on snapshot to a get doc but that also gets the same error. I tried making it so it didn't connect to the emulator that fixed it. I'm not sure why but I like to use the firebase emulator. Is there anyway to fix this

Thanks in advance!

1 Answers

The error message “FirebaseError : false for ‘get’ @L5” is saying that security rules are denying the request at line 5 of your security rules. If you have curated your rules like below,

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

then line 5 has the error as you are not allowing read and write access to anyone. To fix that you can either change your firestore rules to :

match /{document=**} { allow read: if true; allow write: if true; } 

but this allows setting your rules to true to both read and write operations, which means that you allow anybody who knows your project ID to read from, and write to your database which is obviously bad, since malicious users can take advantage of it. It’s true that you can use these settings for a small amount of time for testing purposes, but never in a production environment.

So there is another option in which you can limit access to your database to a fixed amount of time. Today is 1.03.2021, we are limiting the access for exactly one day by:

rules_version = '2';
service cloud.firestore {
 match /databases/{database}/documents {
   match /{document=**} {
     allow read, write: if request.time < timestamp.date(2021, 3, 2);
   }
 }
}

The most important part when it comes to security rules is the Firebase Authentication, meaning that you can allow access only to the users that are authenticated to perform operations in your database. The rules should look like this:

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

If you need a more granular set of rules, for instance, to allow only the authenticated users, who have the value of the UID equal to the value of UID that comes from to authentication process, to be able to write to their own document, then you should consider using the following rules:

rules_version = '2';
service cloud.firestore {
 match /databases/{database}/documents {
   match /users/{uid} {
     allow create: if request.auth != null;
     allow read, update, delete: if request.auth != null && request.auth.uid == uid;
   }
 }
}

A feature request is filed for this error message to be more precise like “FirebaseError: PERMISSION_DENIED because security rules returned false for 'get' @ L5”

Related