0
0
Firebasecloud~5 mins

Custom functions in rules in Firebase - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a custom function in Firebase security rules?
A custom function is a reusable block of code inside Firebase security rules that helps simplify and organize rule logic by grouping repeated checks or calculations.
Click to reveal answer
beginner
How do you define a custom function in Firebase rules?
You define a custom function using the <code>function</code> keyword followed by the function name and parameters inside parentheses and curly braces. For example: <br><code>function isOwner(userId) { return userId == request.auth.uid; }</code>
Click to reveal answer
beginner
Why use custom functions in Firebase rules?
Custom functions make rules easier to read, maintain, and avoid repeating the same logic multiple times. They help keep rules clean and reduce errors.
Click to reveal answer
intermediate
Can custom functions in Firebase rules access request data?
Yes, custom functions can access request and resource objects to check authentication, data values, or other request details needed for security decisions.
Click to reveal answer
intermediate
Example: What does this custom function do?<br><code>function isAdmin() { return request.auth.token.admin == true; }</code>
This function checks if the authenticated user has an admin flag set to true in their authentication token, meaning they have admin privileges.
Click to reveal answer
What keyword is used to define a custom function in Firebase security rules?
Adef
Bfunction
Cfunc
Dlambda
Can custom functions in Firebase rules help reduce repeated code?
AYes, they make rules cleaner and reusable
BNo, they increase code duplication
COnly if you use external libraries
DOnly for read operations
Which object can custom functions access to check the user's identity?
Astorage.bucket
Bresource.data
Cdatabase.ref
Drequest.auth
What is a good reason to use custom functions in Firebase rules?
ATo store user data
BTo slow down rule evaluation
CTo make rules easier to read and maintain
DTo bypass security checks
Which of these is a valid custom function call in Firebase rules?
AisOwner(resource.data.userId)
Bcall isOwner(resource.data.userId)
Cinvoke isOwner(resource.data.userId)
Drun isOwner(resource.data.userId)
Explain what custom functions are in Firebase security rules and why they are useful.
Think about how you avoid repeating yourself in code.
You got /4 concepts.
    Describe how you would write a custom function to check if the current user is the owner of a data item.
    Focus on comparing user IDs.
    You got /4 concepts.