📖 Scenario: You are building a simple user profile storage system using Firebase Realtime Database. Each user has a unique ID, a name, and an email address.
🎯 Goal: Create a Firebase Realtime Database structure to store user profiles, add a configuration variable for the default user role, write the logic to add a new user profile, and finalize the database rules to secure the data.
📋 What You'll Learn
Create a dictionary called users with two user profiles with exact IDs and data
Add a variable called defaultRole set to "user"
Write a function called addUserProfile that takes userId, name, and email and adds a new user with the defaultRole
Add Firebase Realtime Database rules to allow authenticated users to read and write their own profile only
💡 Why This Matters
🌍 Real World
Storing and managing user profiles securely is essential for many apps and websites. Firebase Realtime Database is a popular backend service for this.
💼 Career
Understanding how to structure user data and secure it with Firebase rules is a key skill for cloud developers and backend engineers.
Progress0 / 4 steps
1
Create initial user profiles
Create a dictionary called users with these exact entries: "user1" with {"name": "Alice", "email": "alice@example.com"} and "user2" with {"name": "Bob", "email": "bob@example.com"}.
Firebase
Hint
Use a dictionary with user IDs as keys and dictionaries with name and email as values.
2
Add default user role configuration
Add a variable called defaultRole and set it to the string "user".
Firebase
Hint
Just create a variable with the exact name and value.
3
Write function to add user profile
Write a function called addUserProfile that takes parameters userId, name, and email. Inside the function, add a new entry to the users dictionary with the given userId and a dictionary containing name, email, and role set to defaultRole.
Firebase
Hint
Define a function with the exact name and parameters, then assign the new user dictionary to users[userId].
4
Add Firebase Realtime Database security rules
Add Firebase Realtime Database rules as a JSON string variable called databaseRules that allows authenticated users to read and write only their own profile under /users/<userId>. Use auth != null and auth.uid == userId conditions.
Firebase
Hint
Use a JSON string with rules for /users/$userId path and conditions for read and write.
Practice
(1/5)
1. What is the main purpose of storing user profile data in Firebase Firestore?
easy
A. To host static website files
B. To run server-side scripts automatically
C. To send emails to users
D. To save personal information for each user securely
Solution
Step 1: Understand user profile data purpose
User profile data holds personal details like name and email for each user.
Step 2: Identify Firebase Firestore role
Firestore stores this data securely and allows easy access and updates.
Final Answer:
To save personal information for each user securely -> Option D
Quick Check:
User profile data = personal info storage [OK]
Hint: User profiles store personal info, not run scripts or host files [OK]
Common Mistakes:
Confusing Firestore with hosting or email services