0Pricing
Firebase Auth & Realtime Database Apps · บทเรียน

การควบคุมการเข้าถึงตามผู้ใช้

ใช้งานกฎเพื่ออนุญาตหรือปฏิเสธการเข้าถึงการอ่านและเขียนตามรหัสผู้ใช้และบทบาทของผู้ใช้ที่ผ่านการยืนยันตัวตน

การควบคุมการเข้าถึงตามผู้ใช้ เป็นบทเรียน Firebase Auth & Realtime Database Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Firebase Auth & Realtime Database Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Control Access by User

Welcome to this lesson! In secure applications, it's crucial to control who can access what data. This is known as User-Based Access Control.

Firebase Realtime Database Security Rules allow you to define precise permissions based on the user who is currently logged in.

Meet the 'auth' Variable

Inside your security rules, Firebase provides a special auth variable. This variable contains information about the currently authenticated user.

  • auth.uid: The unique ID of the logged-in user.
  • auth.token: An object containing custom claims and other token details (e.g., email).

If no user is logged in, auth will be null.

Authenticated Users Only

The simplest form of user-based access is to ensure only authenticated users can read or write any data.

You can achieve this by checking if the auth variable is not null.

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

Users Read Their Own Data

Often, you want users to only read data that belongs to them. Imagine a /users node where each user has a sub-node with their UID.

We can use a wildcard variable ($uid) in the path to match the current user's ID.

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "auth.uid == $uid"
      }
    }
  }
}

Users Write Their Own Data

Similarly, you can restrict write access so users can only modify their own data. This prevents one user from changing another's profile.

The rule is very similar to the read rule, just applied to .write.

{
  "rules": {
    "users": {
      "$uid": {
        ".write": "auth.uid == $uid"
      }
    }
  }
}

Read & Write Your Own Profile

Let's combine the read and write rules. This common pattern allows users full control over their own specific data node, often used for user profiles.

Here, $userId is a placeholder for an actual user's UID.

{
  "rules": {
    "profiles": {
      "$userId": {
        ".read": "auth.uid == $userId",
        ".write": "auth.uid == $userId"
      }
    }
  }
}

Post Ownership Example

Consider a 'posts' section where anyone can read posts, but only the creator can edit or delete their own post.

We assume each post object has an ownerId field. We use data.ownerId to refer to the existing owner ID in the database.

{
  "rules": {
    "posts": {
      "$postId": {
        ".read": "true",
        ".write": "auth.uid == data.ownerId"
      }
    }
  }
}

Validating Data with Auth

Beyond just who can write, you can also validate what data they write. For instance, ensuring that when a user creates an item, they correctly set themselves as the owner.

The newData variable refers to the data being written.

{
  "rules": {
    "items": {
      "$itemId": {
        ".write": "auth != null",
        ".validate": "newData.ownerId == auth.uid"
      }
    }
  }
}

Introducing User Roles

For more complex access, you can define roles like 'admin' or 'moderator'. These roles are often stored as custom claims in the user's authentication token.

You can then check for these roles in your rules using auth.token.

{
  "rules": {
    "adminContent": {
      ".read": "auth.token.isAdmin == true",
      ".write": "auth.token.isAdmin == true"
    }
  }
}

Quick Check on Access

Consider the following Realtime Database Security Rules:

{ "rules": { "messages": { "$messageId": { ".read": "auth.uid == data.senderId", ".write": "auth.uid == data.senderId" } } } }

If user "user123" is authenticated and tries to read a message where data.senderId is "user456", will they succeed?

Recap: User Access Rules

You've learned how to implement powerful user-based access control in Firebase Realtime Database Security Rules!

  • The auth variable provides current user details.
  • You can restrict access to authenticated users (auth != null).
  • Users can be granted read/write access to their own specific data using auth.uid == $uid.
  • You can validate incoming data using newData and auth.uid.
  • Roles can be used to grant access to specific user groups.

Next, explore how to validate the data itself!

คำถามที่พบบ่อย

บทเรียน “การควบคุมการเข้าถึงตามผู้ใช้” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การควบคุมการเข้าถึงตามผู้ใช้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Firebase Auth & Realtime Database Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การควบคุมการเข้าถึงตามผู้ใช้”

ใช้งานกฎเพื่ออนุญาตหรือปฏิเสธการเข้าถึงการอ่านและเขียนตามรหัสผู้ใช้และบทบาทของผู้ใช้ที่ผ่านการยืนยันตัวตน คุณปฏิบัติ Firebase Auth & Realtime Database Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Firebase Auth & Realtime Database Apps หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Firebase Auth & Realtime Database Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การควบคุมการเข้าถึงตามผู้ใช้” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Firebase Auth & Realtime Database Apps นี้ได้ไหม

ได้ บทเรียน Firebase Auth & Realtime Database Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ทำความเข้าใจไวยากรณ์ของกฎความปลอดภัย
  2. การควบคุมการเข้าถึงตามผู้ใช้
  3. การตรวจสอบความถูกต้องของข้อมูลด้วยกฎ
  4. การทดสอบและแก้ไขข้อบกพร่องของกฎความปลอดภัย
← กลับไปที่ Firebase Auth & Realtime Database Apps