Bird
Raised Fist0
Firebasecloud~20 mins

Authentication-based rules in Firebase - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Firebase Auth Rules Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
Firebase rule behavior with authenticated users

Consider this Firebase Realtime Database rule snippet:

{
  "rules": {
    "messages": {
      "$messageId": {
        ".read": "auth != null",
        ".write": "auth != null && auth.uid == data.child('owner').val()"
      }
    }
  }
}

What happens when a logged-in user tries to write a message where the 'owner' field is not equal to their user ID?

AThe write is denied because the user is not the owner of the existing message.
BThe write is allowed because the user is authenticated.
CThe write is denied because the user is not authenticated.
DThe write is allowed only if the message does not exist yet.
Attempts:
2 left
💡 Hint

Check the condition for '.write' and what 'data.child('owner').val()' means.

🧠 Conceptual
intermediate
1:30remaining
Understanding Firebase auth variable in rules

In Firebase Realtime Database security rules, what does the auth variable represent?

AThe list of all users currently connected to the database.
BThe database reference path of the current read or write operation.
CThe server timestamp when the request is processed.
DThe authentication token of the current user making the request, or null if unauthenticated.
Attempts:
2 left
💡 Hint

Think about how Firebase identifies who is making a request.

❓ security
advanced
2:30remaining
Preventing unauthorized data deletion in Firebase

Given this Firebase rule:

{
  "rules": {
    "users": {
      "$uid": {
        ".write": "auth != null && auth.uid == $uid"
      }
    }
  }
}

Which of the following actions is not allowed by this rule?

AA user creating their own profile data.
BA user deleting their own profile data.
CA user updating another user's profile data.
DA user updating their own profile data.
Attempts:
2 left
💡 Hint

Check the condition comparing auth.uid and $uid.

❓ Configuration
advanced
2:30remaining
Configuring Firebase rules for public read and authenticated write

You want to allow anyone to read data under /posts but only authenticated users to write. Which rule configuration achieves this?

A
{
  "rules": {
    "posts": {
      ".read": "auth != null",
      ".write": "true"
    }
  }
}
B
{
  "rules": {
    "posts": {
      ".read": "true",
      ".write": "auth != null"
    }
  }
}
C
{
  "rules": {
    "posts": {
      ".read": "auth == null",
      ".write": "auth != null"
    }
  }
}
D
{
  "rules": {
    "posts": {
      ".read": "false",
      ".write": "auth != null"
    }
  }
}
Attempts:
2 left
💡 Hint

Remember true means anyone can read.

❓ Architecture
expert
3:00remaining
Designing Firebase rules for multi-role access control

You have a Firebase Realtime Database with a /projects node. Each project has an ownerId and a list of collaborators (user IDs). You want to allow:

  • Owners to read and write their projects.
  • Collaborators to read but not write.
  • Others no access.

Which rule snippet correctly implements this?

A
{
  "rules": {
    "projects": {
      "$projectId": {
        ".read": "auth != null && (auth.uid == data.child('ownerId').val() || data.child('collaborators').hasChild(auth.uid))",
        ".write": "auth != null && auth.uid == data.child('ownerId').val()"
      }
    }
  }
}
B
{
  "rules": {
    "projects": {
      "$projectId": {
        ".read": "auth != null && auth.uid == data.child('ownerId').val()",
        ".write": "auth != null && (auth.uid == data.child('ownerId').val() || data.child('collaborators').hasChild(auth.uid))"
      }
    }
  }
}
C
{
  "rules": {
    "projects": {
      "$projectId": {
        ".read": "true",
        ".write": "auth != null && auth.uid == data.child('ownerId').val()"
      }
    }
  }
}
D
{
  "rules": {
    "projects": {
      "$projectId": {
        ".read": "auth != null",
        ".write": "auth != null && auth.uid == data.child('ownerId').val()"
      }
    }
  }
}
Attempts:
2 left
💡 Hint

Think about who can read and who can write, and how to check collaborators.

Practice

(1/5)
1. What does the Firebase rule condition request.auth != null check for?
easy
A. If the request is from a mobile device
B. If the user has admin privileges
C. If the user is signed in
D. If the database is empty

Solution

  1. Step 1: Understand the meaning of request.auth

    request.auth holds authentication info if a user is signed in; otherwise, it is null.
  2. Step 2: Interpret the condition request.auth != null

    This condition is true only when a user is signed in, meaning authentication info exists.
  3. Final Answer:

    If the user is signed in -> Option C
  4. Quick Check:

    Authentication check = user signed in [OK]
Hint: Check if request.auth is not null to confirm user login [OK]
Common Mistakes:
  • Thinking it checks for admin rights
  • Assuming it checks device type
  • Confusing with database content check
2. Which of the following is the correct Firebase rule syntax to allow read access only to authenticated users?
easy
A. allow read: if request.auth == true;
B. allow read: if request.auth == null;
C. allow read: if request.auth.id == null;
D. allow read: if request.auth != null;

Solution

  1. Step 1: Identify the correct condition for authenticated users

    The condition request.auth != null means the user is signed in.
  2. Step 2: Match the syntax with the correct condition

    allow read: if request.auth != null; uses allow read: if request.auth != null; which correctly allows reads only for signed-in users.
  3. Final Answer:

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

    Correct syntax for auth check = allow read: if request.auth != null; [OK]
Hint: Use != null to allow only signed-in users [OK]
Common Mistakes:
  • Using == null which allows unauthenticated users
  • Checking request.auth.id instead of request.auth
  • Using boolean true instead of null check
3. Given this Firebase rule snippet:
allow read: if request.auth != null && request.auth.uid == resource.data.ownerId;

What happens if a signed-in user tries to read data where ownerId is different from their uid?
medium
A. Read access is denied
B. Read access is granted only if user is admin
C. Read access is granted
D. Read access causes a syntax error

Solution

  1. Step 1: Analyze the rule condition

    The rule requires the user to be signed in and their user ID (uid) to match the data's ownerId.
  2. Step 2: Evaluate the scenario where uid differs from ownerId

    Since the IDs do not match, the condition fails, so access is denied.
  3. Final Answer:

    Read access is denied -> Option A
  4. Quick Check:

    User ID match required = access denied if no match [OK]
Hint: User ID must match ownerId for access [OK]
Common Mistakes:
  • Assuming any signed-in user can read
  • Thinking admin rights are checked here
  • Confusing syntax error with logic denial
4. Identify the error in this Firebase rule:
allow write: if request.auth.uid = resource.data.ownerId;
medium
A. Missing check for request.auth != null
B. Using single equals (=) instead of double equals (==)
C. Incorrect field name 'ownerId'
D. No error, rule is correct

Solution

  1. Step 1: Check the comparison operator

    The rule uses a single equals = which is an assignment, not a comparison.
  2. Step 2: Correct operator usage in Firebase rules

    Firebase rules require double equals == for equality checks.
  3. Final Answer:

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

    Use == for comparisons, not = [OK]
Hint: Use == for equality, not = [OK]
Common Mistakes:
  • Confusing assignment (=) with comparison (==)
  • Ignoring null check for request.auth
  • Assuming field names cause error
5. You want to write a Firebase rule that allows users to update their own profile only if they are signed in and the profile's userId matches their uid. Which rule correctly implements this?
hard
A. allow update: if request.auth != null && request.auth.uid == resource.data.userId;
B. allow update: if request.auth != null && request.auth.uid == request.resource.data.userId;
C. allow update: if request.auth == null || request.auth.uid != resource.data.userId;
D. allow update: if request.auth != null && request.auth.uid == resource.data.ownerId;

Solution

  1. Step 1: Understand the difference between resource.data and request.resource.data

    resource.data is the existing data; request.resource.data is the new data being written.
  2. Step 2: Match user ID with existing data's userId during update

    To allow update only if the profile's existing userId matches the user's uid, compare request.auth.uid with resource.data.userId.
  3. Step 3: Verify the condition

    allow update: if request.auth != null && request.auth.uid == resource.data.userId; correctly checks the user is signed in and owns the existing profile.
  4. Final Answer:

    allow update: if request.auth != null && request.auth.uid == resource.data.userId; -> Option A
  5. Quick Check:

    Check existing data userId matches auth.uid for update [OK]
Hint: Use resource.data for existing ownership checks [OK]
Common Mistakes:
  • Using request.resource.data (new data) instead of resource.data (existing data)
  • Allowing updates without authentication
  • Using wrong field names for user ID
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