0
0
Firebasecloud~10 mins

Role-based access control pattern in Firebase - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a user role in Firebase rules.

Firebase
match /users/{userId} {
  allow read: if request.auth.token.role == [1];
}
Drag options to blanks, or click blank then click option'
A"admin"
B"user"
C"guest"
D"editor"
Attempts:
3 left
💡 Hint
Common Mistakes
Using role names without quotes
Using a role that is not defined in the system
2fill in blank
medium

Complete the code to check if the user has the editor role.

Firebase
allow write: if request.auth.token.role == [1];
Drag options to blanks, or click blank then click option'
A"admin"
B"editor"
C"user"
D"guest"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong role for write access
Forgetting to use quotes around the role
3fill in blank
hard

Fix the error in the role check to allow only admins to delete.

Firebase
allow delete: if request.auth.token.role == [1];
Drag options to blanks, or click blank then click option'
A"admin"
B"guest"
C"user"
D"editor"
Attempts:
3 left
💡 Hint
Common Mistakes
Using single '=' instead of '==' for comparison
Using the wrong role string
4fill in blank
hard

Fill both blanks to allow read for admins and editors only.

Firebase
allow read: if request.auth.token.role == [1] || request.auth.token.role == [2];
Drag options to blanks, or click blank then click option'
A"admin"
B"user"
C"editor"
D"guest"
Attempts:
3 left
💡 Hint
Common Mistakes
Using roles that should not have read access
Using '&&' instead of '||' for this condition
5fill in blank
hard

Fill all three blanks to allow write only if user is admin and the document owner.

Firebase
allow write: if request.auth.token.role == [1] && resource.data.ownerId == [2] && request.auth.uid == [3];
Drag options to blanks, or click blank then click option'
A"admin"
Bresource.data.ownerId
Crequest.auth.uid
D"user"
Attempts:
3 left
💡 Hint
Common Mistakes
Not checking ownership correctly
Using wrong role or variables