การควบคุมการเข้าถึงข้อมูลผู้ใช้ตามบทบาท
ผสานบทบาทจาก Firebase Auth เข้ากับกฎของ Realtime Database เพื่อมอบระดับการเข้าถึงข้อมูลที่แชร์และข้อมูลส่วนตัวแตกต่างกันแก่ผู้ดูแล สมาชิก และผู้เยี่ยมชม
การควบคุมการเข้าถึงข้อมูลผู้ใช้ตามบทบาท เป็นบทเรียน Firebase Auth & Realtime Database Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Firebase Auth & Realtime Database Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Beyond Owner-Only Access
So far each user reads and writes their own data. Real apps need roles: an admin who moderates content, members who collaborate, and guests with read-only access.
Role-based access control (RBAC) layers permissions on top of authentication.
Where Roles Live
You can store a user's role in two places:
- A
rolesnode in the database, read inside rules - A custom claim on the auth token (set server-side)
Custom claims are faster to check; database roles are easier to change at runtime.
Roles in the Database
A simple model maps each uid to a role string. This data itself must be locked down so users cannot promote themselves.
{
"roles": {
"uid_alice": "admin",
"uid_bob": "member"
}
}Checking a Database Role in Rules
Rules can read other parts of the database with root. Here only admins may write to a shared config node.
{
"rules": {
"config": {
".write": "root.child('roles').child(auth.uid).val() === 'admin'"
}
}
}Custom Claims for Roles
With the Admin SDK you can attach a role to the token itself. This is checked without an extra database read.
await admin.auth().setCustomUserClaims(uid, { role: 'admin' });Checking Claims in Rules
Custom claims appear under auth.token. The rule becomes simpler and avoids a root lookup.
{
"rules": {
"config": {
".write": "auth.token.role === 'admin'"
}
}
}Reading the Claim Client-Side
The client can read its own claims to adjust the UI, for example showing an admin panel only to admins.
import { getAuth, getIdTokenResult } from 'firebase/auth';
const res = await getIdTokenResult(getAuth().currentUser);
if (res.claims.role === 'admin') showAdminPanel();Tiered Read Access
Different roles can have different read scopes. Members read shared docs; guests read only public ones.
{
"rules": {
"shared": {
".read": "auth.token.role === 'member' || auth.token.role === 'admin'"
}
}
}Protecting the Role Data Itself
Critically, users must not be able to edit their own role. Make the roles node writable only by admins (or only server-side), or self-escalation defeats the whole system.
{
"rules": {
"roles": {
".write": "auth.token.role === 'admin'"
}
}
}Claim Propagation Delay
After you change a custom claim, the user's existing token still has the old value until it refreshes (about an hour, or on forced refresh). Call getIdToken(true) client-side to pick up new roles immediately.
await getAuth().currentUser.getIdToken(true);Choosing an Approach
Use custom claims for stable, security-critical roles, and database roles when permissions change often or need to be queried. Many apps combine both.
Quick Check
Test your understanding of role-based access.
Recap
You can now grant tiered access by role.
- Store roles in the database or as custom claims
- Check database roles via
root, claims viaauth.token - Give roles different read/write scopes
- Lock down the role data so users cannot self-promote
- Refresh tokens to pick up new claims promptly
เรียนรู้ Firebase Auth & Realtime Database Apps ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 11
- บทเรียน
- 44
คำถามที่พบบ่อย
บทเรียน “การควบคุมการเข้าถึงข้อมูลผู้ใช้ตามบทบาท” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การควบคุมการเข้าถึงข้อมูลผู้ใช้ตามบทบาท” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Firebase Auth & Realtime Database Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การควบคุมการเข้าถึงข้อมูลผู้ใช้ตามบทบาท”
ผสานบทบาทจาก Firebase Auth เข้ากับกฎของ Realtime Database เพื่อมอบระดับการเข้าถึงข้อมูลที่แชร์และข้อมูลส่วนตัวแตกต่างกันแก่ผู้ดูแล สมาชิก และผู้เยี่ยมชม คุณปฏิบัติ Firebase Auth & Realtime Database Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Firebase Auth & Realtime Database Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Firebase Auth & Realtime Database Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การควบคุมการเข้าถึงข้อมูลผู้ใช้ตามบทบาท” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Firebase Auth & Realtime Database Apps นี้ได้ไหม
ได้ บทเรียน Firebase Auth & Realtime Database Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเชื่อมโยงข้อมูลผู้ใช้กับการยืนยันตัวตน
- โปรไฟล์ผู้ใช้แบบเรียลไทม์
- การแก้ไขข้อมูลร่วมกัน
- การควบคุมการเข้าถึงข้อมูลผู้ใช้ตามบทบาท