المطالبات المخصصة وقواعد الأمان
حدّد مطالبات مخصصة للمستخدمين وادمجها مع Firebase Security Rules للتحكم في الوصول إلى الموارد
المطالبات المخصصة وقواعد الأمان درس مجاني في Firebase Auth & Realtime Database Apps على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Firebase Auth & Realtime Database Apps، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Firebase Auth & Realtime Database Apps 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Intro to Custom Claims
Beyond basic user authentication, Firebase allows you to define Custom Claims. These are key-value pairs that you can add to a user's ID token, providing extra information about the user.
Think of them as custom labels or badges attached to a user's identity.
Why Use Custom Claims?
Custom claims are powerful for implementing role-based access control (RBAC) or granting specific permissions within your app. Instead of just knowing 'who' the user is, you can know 'what' they are allowed to do.
- Designate users as 'admin', 'editor', or 'subscriber'.
- Grant access to premium features or content.
- Control data access based on custom attributes.
Setting Claims (Server-Side)
Custom claims are sensitive and must be set by a trusted environment, like your backend server, using the Firebase Admin SDK. This prevents malicious users from giving themselves elevated privileges.
When claims are set, the user's ID token is updated. Clients need to refresh their token to receive the new claims.
Simulating Claim Setting
This conceptual example shows how claims work. On a real backend, the Admin SDK would update a user's profile, and these claims would then be available in their ID token.
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String userId = "user123";
Map<String, Object> claims = new HashMap<>();
// --- Server-side action (simulated) ---
System.out.println("Server sets claims for " + userId);
claims.put("role", "admin");
claims.put("level", "premium");
// --- Client-side action (simulated after token refresh) ---
System.out.println("\nClient receives ID token with claims:");
System.out.println("User ID: " + userId);
System.out.println("Claims: " + claims);
// Client checks for specific claim
if (claims.containsKey("role") && claims.get("role").equals("admin")) {
System.out.println("Access check: User is an admin.");
} else {
System.out.println("Access check: Not an admin.");
}
}
}Accessing Claims (Client-Side)
Once a user is logged in and their ID token is refreshed (e.g., after login or explicitly refreshing), your client-side application can read these custom claims from the token.
The claims are embedded within the ID token, which is a JWT (JSON Web Token).
How Claims Power Rules
The true power of custom claims comes when you combine them with Firebase Security Rules. Any custom claim you set on a user's ID token is automatically available within your security rules.
This allows you to create highly specific and dynamic access control logic for your Realtime Database or Cloud Firestore.
Rule Example: Admin Access
Here's how a Firebase Realtime Database Security Rule might use a custom admin: true claim to restrict access to a specific data path.
Only users with this claim in their token would be able to read or write to /adminContent.
{
"rules": {
"adminContent": {
// Only users with 'admin: true' claim can read/write
".read": "auth.token.admin === true",
".write": "auth.token.admin === true"
},
"publicContent": {
// Anyone authenticated can read, no special claims needed
".read": "auth != null",
".write": "false"
}
}
}Rule Example: Premium Content
You can also use claims for different levels of access. This rule grants read access to /premiumContent only if the user has a level: 'premium' claim.
This is much more flexible than just checking if a user is logged in.
{
"rules": {
"premiumContent": {
// Only users with 'level: premium' claim can read
".read": "auth.token.level === 'premium'",
".write": "false"
},
"users": {
"$uid": {
".read": "auth.uid === $uid",
".write": "auth.uid === $uid"
}
}
}
}Best Practices for Claims
To ensure efficient and secure use of custom claims:
- Keep claims small: ID tokens have size limits.
- Don't store sensitive data: Claims are base64 encoded, not encrypted.
- Use for authorization: Not for general data storage.
- Token refresh: Remind users to refresh their ID token if claims change.
Claims Quiz
You've learned how custom claims enhance user roles and security rules. Which statement accurately describes a key aspect of Firebase Custom Claims?
Recap: Custom Claims & Rules
This lesson covered Firebase Custom Claims, a powerful feature for advanced user management.
- Custom claims allow you to add custom attributes to user ID tokens.
- They are set securely using the Firebase Admin SDK on your backend.
- These claims are seamlessly integrated with Firebase Security Rules, enabling robust, role-based access control for your app's resources.
- Always remember to refresh the client's ID token for changes to take effect.
الأسئلة الشائعة
هل درس «المطالبات المخصصة وقواعد الأمان» مجاني؟
نعم — نص درس «المطالبات المخصصة وقواعد الأمان» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Firebase Auth & Realtime Database Apps، انتقل إلى CoddyKit PRO. تتضمن دورة Firebase Auth & Realtime Database Apps 4 دروس في المجموع.
ماذا ستتعلم في «المطالبات المخصصة وقواعد الأمان»؟
حدّد مطالبات مخصصة للمستخدمين وادمجها مع Firebase Security Rules للتحكم في الوصول إلى الموارد تتمرن على Firebase Auth & Realtime Database Apps مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Firebase Auth & Realtime Database Apps؟
لا تُشترط خبرة سابقة. Firebase Auth & Realtime Database Apps على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «المطالبات المخصصة وقواعد الأمان»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Firebase Auth & Realtime Database Apps هذا؟
نعم. كل درس في Firebase Auth & Realtime Database Apps يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- المصادقة برقم الهاتف
- المصادقة متعددة العوامل (MFA)
- المطالبات المخصصة وقواعد الأمان
- ربط الحسابات وإدارة مزوّدي المصادقة