Here are my rules for the firestore.
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth!=null;
}
}
}
When I set allow read, write: if true; I can access the database. However, when I change it to the code above. I get: FirebaseError: Missing or insufficient permissions.
I have read a lot of questions about this error and the only solution which works is allowing read permissions for everyone (I think this would be bad).
How do I authenticated myself (the owner)? Below is the code used to get the document.
var firebaseConfig = {
apiKey: "XXXXXXXXXXXXXXXXXXXXXX",
authDomain: "XXXXXXXXXXXXXXXXX",
projectId: "XXXXXXXXXXXX",
storageBucket: "XXXXXXXXXXXXXXXXX",
messagingSenderId: "XXXXXXXXXXXXXXX",
appId: "XXXXXXXXXXXXXXXXXXXX",
measurementId: "XXXXXXXXXXXX"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
var db = getFirestore();
console.log(db)
const docRef = doc(db,"fta-2up-prob", "fta-2up-prob");
console.log(docRef)
const snapshot = await getDoc(docRef);
console.log(snapshot)
As said before this code does work if I allow anyone to read the database. Any advice would be really appreciated even if it seems obvious (I'm extremely new to this).