เมื่อฉันดึงเอกสารโดยตรงโดยใช้ .doc().get() การรักษาความปลอดภัย firestore จะปฏิเสธการอนุญาต ฉันต้องทำอย่างไรเพื่ออนุญาตการดำเนินการ CRUD สำหรับผู้ใช้ที่ได้รับการรับรองความถูกต้องของแอป Ionic ของฉัน ฉันมีฟังก์ชันต่อไปนี้ในแอป PWA Ionic
``` async getUserProfile(): Promise<firebase.firestore.DocumentSnapshot> {
const user: firebase.User = await this.authProvider.getUser();
this.currentUser = user;
console.log('found current user');
this.userProfile = firebase.firestore().doc(`userProfile/${user.uid}`);
console.log('returing userprofile', this.userProfile.get());
return this.userProfile.get();
}```
แต่การเรียกไปยัง this.userprofile.get() ล้มเหลวเนื่องจากคำเตือนด้านความปลอดภัยจากกฎความปลอดภัยของ Firestore
พบผู้ใช้ปัจจุบันที่คืนโปรไฟล์ผู้ใช้ ZoneAwarePromise __zone_สัญลักษณ์__สถานะ: 0 __zone_สัญลักษณ์__ค่า: FirebaseError: การอนุญาตที่ขาดหายไปหรือไม่เพียงพอ ที่ e ใหม่ (http://localhost:8100/vendor.js:92760:23) ที่ http:// localhost:8100/vendor.js:102987:28 . . . (http://localhost:8100/polyfills.js:10248:56) รหัส: ชื่อที่ถูกปฏิเสธสิทธิ์: FirebaseError toString: ƒ () ข้อความ: สิทธิ์ขาดหายไปหรือไม่เพียงพอ stack: FirebaseError: สิทธิ์ขาดหายไปหรือไม่เพียงพอ\n ที่ e ใหม่
กฎความปลอดภัยของ Firestore ได้รับการตั้งค่าง่ายๆ ดังนี้
service cloud.firestore {
match /databases/{database}/documents {
match /userProfile/{userId}/{documents=**} {
allow read, update, get,delete: if request.auth != null && request.auth.uid == userId;
allow create: if request.auth != null;
}
}
}
กฎความปลอดภัยที่ถูกต้องควรนำไปใช้อย่างไรเพื่ออนุญาตให้ผู้ใช้ที่ได้รับการรับรองความถูกต้องสามารถ CRUD เอกสารของตนได้ หรือวิธีที่ถูกต้องในการดึงเอกสารจากการรวบรวม