Bảo mật dữ liệu bằng ngôn ngữ tự nhiên
Security Rules là "hàng rào" bảo vệ database. Thay vì tự viết code phức tạp, hãy "vibe" với Gemini.
Tình huống thực tế: Bạn cần đảm bảo:
- Chỉ user đã đăng nhập mới đọc được profiles
- User chỉ sửa được HỒ SƠ CỦA CHÍNH MÌNH
- Ai cũng xem được jobs, nhưng chỉ admin mới tạo/sửa
- Lessons chỉ xem được nếu đã enroll khóa học
Prompt mẫu: "Viết Firestore Security Rules cho:
- Collection profiles: Chỉ owner được update, mọi authenticated user được read
- Collection jobs: Public read, chỉ user có role=admin trong profiles mới được write
- Collection courses/modules/lessons: Read nếu user đã enroll (check enrollments collection)"
Gemini trả về rules hoàn chỉnh:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Profiles: Owner-only write, authenticated read
match /profiles/{userId} {
allow read: if request.auth != null;
allow write: if request.auth.uid == userId;
}
// Jobs: Public read, admin-only write
match /jobs/{jobId} {
allow read: if true;
allow write: if get(/databases/$(database)/documents/profiles/$(request.auth.uid)).data.role == 'admin';
}
// Courses: Check enrollment
match /courses/{courseId} {
allow read: if exists(/databases/$(database)/documents/enrollments/$(request.auth.uid + '_' + courseId));
}
}
}
Workflow thực tế:
- Paste rules vào Firebase Console > Firestore > Rules
- Click "Publish"
- Test bằng Rules Playground ngay trong Console
