I have a DB like this:
I want logged in users to be able to read and write to this collection. I also want users who are not logged in to only read this collection..
here is my code
service cloud.firestore {
match /databases/{database}/documents {
// match /{document=**} {
// allow read, write: if request.auth != null;
// }
allow read, write: if request.auth != null;
allow read: if request.auth == null;
}
}
Ive tried other ways and I keep getting FirebaseError: Missing or insufficient permissions.
Any idea?
request to push the code...
import {
initializeApp
} from ".1.0/firebase-app.js";
import {
getStorage,
ref,
uploadBytes,
getDownloadURL
} from ".1.0/firebase-storage.js";
import {
getFirestore,
addDoc,
doc,
collection
} from '.1.0/firebase-firestore.js'
var firebaseConfig = {
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: ""
};
const app = initializeApp(firebaseConfig);
const storage = getStorage(app);
const db = getFirestore(app);
const storageRef = ref(storage, `${recipeId}`);
await uploadBytes(storageRef, file);
const imageURL = await getDownloadURL(storageRef);
var recipeImageUrl = URL + recipeId + "?alt=media"
var date = new Date();
const createdTime = date.toLocaleString("en-US", {
timeZone: "America/New_York"
});
var titleCaseName = titleCase(addRecipeName);
// Add a new document with a generated id.
const docRef = await addDoc(collection(db, "AddedRecipes"), {
...
});
I have a DB like this:
I want logged in users to be able to read and write to this collection. I also want users who are not logged in to only read this collection..
here is my code
service cloud.firestore {
match /databases/{database}/documents {
// match /{document=**} {
// allow read, write: if request.auth != null;
// }
allow read, write: if request.auth != null;
allow read: if request.auth == null;
}
}
Ive tried other ways and I keep getting FirebaseError: Missing or insufficient permissions.
Any idea?
request to push the code...
import {
initializeApp
} from "https://www.gstatic.com/firebasejs/11.1.0/firebase-app.js";
import {
getStorage,
ref,
uploadBytes,
getDownloadURL
} from "https://www.gstatic.com/firebasejs/11.1.0/firebase-storage.js";
import {
getFirestore,
addDoc,
doc,
collection
} from 'https://www.gstatic.com/firebasejs/11.1.0/firebase-firestore.js'
var firebaseConfig = {
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: ""
};
const app = initializeApp(firebaseConfig);
const storage = getStorage(app);
const db = getFirestore(app);
const storageRef = ref(storage, `${recipeId}`);
await uploadBytes(storageRef, file);
const imageURL = await getDownloadURL(storageRef);
var recipeImageUrl = URL + recipeId + "?alt=media"
var date = new Date();
const createdTime = date.toLocaleString("en-US", {
timeZone: "America/New_York"
});
var titleCaseName = titleCase(addRecipeName);
// Add a new document with a generated id.
const docRef = await addDoc(collection(db, "AddedRecipes"), {
...
});
Your read and write rules are at the wrong nesting level. You need a to match specific documents in a way similar to the rules you commented out, then apply the rules under the match
that matches the document. If you want to apply permissions to all documents in a specific collection, you need to match them explicitly using the name of the collection and a wildcard specifier for the document itself:
service cloud.firestore {
match /databases/{database}/documents {
match /AddedRecipes/{doc} {
allow write: if request.auth != null;
allow read: if true;
}
}
}
Also note above that if you want any user to read documents in a collection, your condition need only be "true".
See the documentation for more details.