0
0
Firebasecloud~10 mins

Role-based access control pattern in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Role-based access control pattern
User tries to access resource
Check user's role
Role allowed?
Grant access
Access result
User requests access, system checks their role, then grants or denies access based on allowed roles.
Execution Sample
Firebase
match /documents/{docId} {
  allow read: if request.auth.token.role == 'admin';
  allow write: if request.auth.token.role == 'editor';
}
This Firebase rule allows reading only if the user role is 'admin' and writing only if the role is 'editor'.
Process Table
StepUser RoleRequested ActionCondition CheckedAccess Result
1adminreadrole == 'admin'Access granted
2adminwriterole == 'editor'Access denied
3editorreadrole == 'admin'Access denied
4editorwriterole == 'editor'Access granted
5viewerreadrole == 'admin'Access denied
6viewerwriterole == 'editor'Access denied
💡 Access is granted only when the user's role matches the required role for the action.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
request.auth.token.roleundefinedadminadmineditoreditorviewerviewer
requested_actionundefinedreadwritereadwritereadwrite
access_grantedfalsetruefalsefalsetruefalsefalse
Key Moments - 3 Insights
Why does an 'admin' user get denied write access in step 2?
Because the write permission requires the role to be 'editor', and the user's role is 'admin' (see execution_table step 2).
Can a 'viewer' user read documents according to these rules?
No, because read access is only allowed for 'admin' role, so 'viewer' role is denied (see execution_table steps 5 and 6).
What happens if the user's role is missing or undefined?
Access will be denied since the role check conditions will fail, ensuring no access without a valid role.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the access result for an 'editor' trying to read?
AAccess pending
BAccess granted
CAccess denied
DRole not found
💡 Hint
Check execution_table row 3 under 'Access Result' column.
At which step does the condition 'role == "editor"' evaluate to true?
AStep 1
BStep 4
CStep 2
DStep 5
💡 Hint
Look at execution_table rows where 'Condition Checked' is 'role == "editor"' and see when access is granted.
If we add a new role 'viewer' with read access, how would step 5 change?
AAccess granted for read
BAccess denied for read
CAccess granted for write
DNo change
💡 Hint
Consider how adding 'viewer' role to read permission affects access in execution_table step 5.
Concept Snapshot
Role-based access control (RBAC) uses user roles to allow or deny access.
In Firebase, rules check request.auth.token.role.
Access is granted only if the user's role matches the required role.
Separate rules can control read and write permissions.
If role is missing or mismatched, access is denied.
Full Transcript
This visual execution shows how role-based access control works in Firebase security rules. When a user requests access to a document, the system checks their role from the authentication token. If the role matches the required role for the requested action (read or write), access is granted. Otherwise, access is denied. For example, only users with the 'admin' role can read, and only users with the 'editor' role can write. Users with other roles like 'viewer' are denied both read and write access. This pattern ensures that users only perform actions allowed by their roles, improving security and control.