Bird
Raised Fist0
Firebasecloud~10 mins

Common rule patterns in Firebase - Interactive Code Practice

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

Complete the code to allow read access only to authenticated users.

Firebase
allow read: if request.auth [1] null;
Drag options to blanks, or click blank then click option'
A<
B==
C!=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' causes the rule to allow only unauthenticated users.
Using comparison operators like '<' or '>' is invalid here.
2fill in blank
medium

Complete the code to allow write access only if the user's UID matches the document ID.

Firebase
allow write: if request.auth.uid [1] resource.id;
Drag options to blanks, or click blank then click option'
A==
B!=
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' allows users to write to documents not matching their UID.
Using '<=' or '>' is not meaningful for string comparison here.
3fill in blank
hard

Fix the error in the rule that allows read access only if the user is authenticated and the document is public.

Firebase
allow read: if request.auth [1] null && resource.data.public == true;
Drag options to blanks, or click blank then click option'
A===
B!=
C==
D!==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' allows unauthenticated users to read.
Using strict equality operators like '===' is not supported in Firebase rules.
4fill in blank
hard

Fill both blanks to allow update only if the user is authenticated and the new data has a 'status' field equal to 'active'.

Firebase
allow update: if request.auth [1] null && request.resource.data.status [2] 'active';
Drag options to blanks, or click blank then click option'
A!=
B==
C===
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' for authentication check allows unauthenticated users.
Using '===' causes syntax errors in Firebase rules.
5fill in blank
hard

Fill all three blanks to allow delete only if the user is authenticated, the document owner matches the user, and the document is not locked.

Firebase
allow delete: if request.auth.uid [1] resource.data.owner && request.auth [2] null && resource.data.locked [3] false;
Drag options to blanks, or click blank then click option'
A==
B!=
D!
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up operators causes the rule to allow unauthorized deletes.
Checking locked with '!=' false would allow locked documents to be deleted.

Practice

(1/5)
1. What is the main purpose of Firebase security rules in a database?
easy
A. To control who can read or write data
B. To speed up database queries
C. To store user passwords securely
D. To backup data automatically

Solution

  1. Step 1: Understand Firebase security rules role

    Firebase security rules define who can access or change data in your database.
  2. Step 2: Identify the main function

    The main function is to control read and write access to protect data.
  3. Final Answer:

    To control who can read or write data -> Option A
  4. Quick Check:

    Security rules = access control [OK]
Hint: Rules control data access permissions quickly [OK]
Common Mistakes:
  • Confusing rules with data backup
  • Thinking rules speed up queries
  • Assuming rules store passwords
2. Which of the following is the correct syntax to allow only authenticated users to read data in Firebase rules?
easy
A. allow read: if request.auth != null;
B. allow read: if user.loggedIn == true;
C. allow read: if auth.user == true;
D. allow read: if request.user != null;

Solution

  1. Step 1: Identify correct Firebase auth syntax

    Firebase uses request.auth to check if a user is authenticated.
  2. Step 2: Match syntax to allow read only if authenticated

    The correct condition is request.auth != null meaning user is logged in.
  3. Final Answer:

    allow read: if request.auth != null; -> Option A
  4. Quick Check:

    request.auth != null means user logged in [OK]
Hint: Use request.auth to check user authentication [OK]
Common Mistakes:
  • Using incorrect variable names like user.loggedIn
  • Checking auth.user instead of request.auth
  • Confusing request.user with request.auth
3. Given this Firebase rule snippet:
allow write: if request.auth.uid == resource.data.ownerId;

What does this rule enforce?
medium
A. No one can write to the data
B. Only the owner of the data can write to it
C. Anyone can write if authenticated
D. Only admins can write to the data

Solution

  1. Step 1: Understand the condition in the rule

    The rule checks if the user's ID matches the ownerId stored in the data.
  2. Step 2: Interpret the effect of the condition

    This means only the user who owns the data (ownerId) can write to it.
  3. Final Answer:

    Only the owner of the data can write to it -> Option B
  4. Quick Check:

    request.auth.uid == resource.data.ownerId means owner only [OK]
Hint: Match request.auth.uid with data ownerId to allow write [OK]
Common Mistakes:
  • Assuming all authenticated users can write
  • Thinking admins are required
  • Believing no one can write
4. Identify the error in this Firebase rule snippet:
allow read: if request.auth.uid = resource.data.ownerId;
medium
A. request.auth.uid is not a valid property
B. Missing semicolon at the end of the line
C. Using single equals (=) instead of double equals (==) for comparison
D. resource.data.ownerId cannot be used in rules

Solution

  1. Step 1: Check the comparison operator used

    The rule uses a single equals sign (=) which is assignment, not comparison.
  2. Step 2: Correct operator for comparison in Firebase rules

    Firebase rules require double equals (==) to compare values.
  3. Final Answer:

    Using single equals (=) instead of double equals (==) for comparison -> Option C
  4. Quick Check:

    Use == for comparison, not = [OK]
Hint: Use == to compare values in rules, not = [OK]
Common Mistakes:
  • Confusing assignment (=) with comparison (==)
  • Thinking semicolon is mandatory
  • Believing request.auth.uid is invalid
5. You want to allow users to update their own profile but prevent them from changing their user ID. Which Firebase rule pattern correctly enforces this?
hard
A. allow update: if request.auth.uid == request.resource.data.userId;
B. allow update: if request.resource.data.userId == null;
C. allow update: if request.auth.uid != resource.data.userId;
D. allow update: if request.auth.uid == resource.data.userId && request.resource.data.userId == resource.data.userId;

Solution

  1. Step 1: Ensure user can update only their own profile

    The rule must check that the authenticated user matches the existing userId in data.
  2. Step 2: Prevent changing the userId field

    Check that the new data's userId matches the old data's userId to prevent changes.
  3. Final Answer:

    allow update: if request.auth.uid == resource.data.userId && request.resource.data.userId == resource.data.userId; -> Option D
  4. Quick Check:

    Match auth.uid and keep userId unchanged [OK]
Hint: Check auth.uid matches and userId stays same [OK]
Common Mistakes:
  • Allowing userId to be changed
  • Not verifying user owns the profile
  • Using != instead of == for ownership check
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard