我有一個與 Firebase 連接的新聞應用程式,我僅在 Firestore 中手動添加新聞,在 5 Collection 內,我不擅長使用 Firebase,我使用測驗模式,現在我需要你的幫助來更改它,該應用程式是用于新聞,這意味著沒有人可以添加它只能讀取每個人的資料甚至沒有登錄,我從谷歌收到了很多安全訊息,請在這里寫代碼,因為我在這里瀏覽了很多問題,我什么都不懂看來我需要一門關于 Firebase 安全的課程。
我使用按次付費的計劃,我害怕高額賬單。請幫助我提供強大的安全性,保護我免受虛假閱讀甚至寫作的侵害。
uj5u.com熱心網友回復:
如果不了解您的 Firestore 集合名稱,就很難回答,但這是一個通用的解決方案。
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// in this example, only admins can write or delete, but anyone can read.
match /news/{docId}{
allow read;
allow write, delete: if isAdmin(request);
}
// This is an example for a users collection, where the doc ID is the
// users authentication Id
// Note the userId=** -> this will apply rules to all subcollections
match /users/{userId=**}{
allow read: if isAuthenticated(request); // logged in users can read
// Only admins or the user with a matching doc Id can write.
allow write: if isAdmin(request) || isOwner(userId);
allow delete: if isAdmin(request); // only admins can delete
}
function isOwner(userID){
return request.auth.uid == userID;
}
function isAuthenticated(request){
return request.auth != null;
}
// this requires you to have set up custom claims
function isAdmin(request) {
return request.auth.token.admin
}
}
}
要了解有關使用基于角色的身份驗證保護您的集合的更多資訊,請查看自定義宣告
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/507499.html
標籤:Google Cloud Collective 火力基地 firebase-安全
上一篇:檢查此函式結果的簡寫是什么?