當新用戶注冊時,我必須檢查是否已經提供了所需的用戶名。因此,我使用以下查詢檢查用戶名:
result = await FirebaseFirestore.instance
.collection('user')
.where('usernameName', isEqualTo: usernameEditController.text)
.get();
userNameExists = result.docs.isEmpty;
如果用戶名已經給出,代碼作業正常并回傳正確的布林值。
現在,firebase 規則存在問題。因為我在登錄時要求輸入用戶名(上面的代碼),所以我必須將 firebase firestore 規則設定為:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;
}
}
}
但是有了這些規則,每個人都可以讀寫。是否有可能在不更改規則的情況下檢查用戶名或使用另一個用戶名查詢?
uj5u.com熱心網友回復:
即使不進行身份驗證,您也可以允許所有用戶檢查“用戶”集合。這不是最佳解決方案,因為任何人都可以閱讀用戶的收藏。
您可以做的另一件事是擁有另一個名為“用戶名”的集合,您將向其中添加新用戶,但唯一存在的資料將是用戶名。
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{collectionName}/{documentId} {
allow read : if (collectionName == "users" || request.auth != null);
allow write : if request.auth != null;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/400053.html
標籤:火力基地 扑 谷歌云firestore 火力基地安全
