Thuta Learning
ရှာဖွေရန်
ProjectsDevOps & Toolsintermediate

Security Rules အခြေခံ

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Security Rules အခြေခံ

Firebase project တစ်ခုမှာ code အလုပ်လုပ်ရုံနဲ့မပြီးပါ။ Database နဲ့ Storage ကို ဘယ်သူဖတ်နိုင်လဲ၊ ဘယ်သူရေးနိုင်လဲဆိုတာ rules နဲ့ထိန်းရပါမယ်။ Rules မမှန်ရင် app ကလှလှပပဖြစ်နေပေမဲ့ backend တံခါးပွင့်နေနိုင်ပါတယ်။

Firestore Owner-based Rule Example

javascript
rules_version = '2';

service cloud.firestore {
  match /databases/{database}/documents {
    match /notes/{noteId} {
      allow read, update, delete: if request.auth != null
        && resource.data.ownerId == request.auth.uid;

      allow create: if request.auth != null
        && request.resource.data.ownerId == request.auth.uid;
    }
  }
}

ဒီ rule က ဘာလုပ်တာလဲ?

Login ဝင်ထားတဲ့ user ပဲ note create/read/update/delete လုပ်နိုင်အောင်စစ်ပါတယ်။ Read/update/delete မှာရှိပြီးသား document ရဲ့ ownerId ကိုစစ်ပြီး create မှာအသစ်ရေးမယ့် data ရဲ့ ownerId ကိုစစ်ပါတယ်။

Common mistake

Learning phase မှာ allow read, write: if true; လို့ဖွင့်ထားပြီး နောက်မှမပိတ်မိတာ အလွန်အန္တရာယ်များပါတယ်။ Test လုပ်ပြီးတာနဲ့ owner-based rules ပြောင်းပါ။

Security Rules အခြေခံ | Thuta Learning