[英]Firebase - How to set security rules so that only logged in users can read and write?
[英]How do I define the security rules where only logged in user can read and write all of the collections and subcollection?
我有category
、 products
和orders
的這些 collections。 然后在products
下我有一個history
子集。 同樣在我的應用程序中,只有一種類型的用戶是我直接在 Firebase 控制台中添加的。 如何定義只有登錄用戶才能讀寫這些 collections 和子集合的安全規則?
對於登錄,我使用 Firebase 身份驗證:
const handleSubmit = async (e) => {
e.preventDefault();
const auth = getAuth();
console.log(email, password, "1");
setIsLoading(true);
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
setIsLoading(false);
navigate("/Homepage");
// ...
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
setIsLoading(false);
alert(errorMessage);
});
};
如何定義只有登錄用戶才能讀寫這些 collections 和子集合的安全規則?
以下規則使用了通配符,應該允許任何經過身份驗證的用戶讀取和寫入 Firestore 中的任何文檔:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
如果你想在某個時候鎖定它,因為你引入了一個並非所有用戶都應該有權訪問的集合,你可以將其明確化:
service cloud.firestore {
match /databases/{database}/documents {
match /category/{id} {
allow read, write: if request.auth != null;
}
match /products/{id} {
allow read, write: if request.auth != null;
}
match /logs/{id} {
allow read, write: if false;
}
}
}
有關更多信息,請從文檔的此處開始,並使用 Firebase 控制台中的 Playground 在部署規則之前測試您的規則。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.