0
0
Firebasecloud~10 mins

User profile data in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - User profile data
User signs up or logs in
Check if profile exists in Firestore
Load profile
User views or edits profile
Save changes to Firestore
Profile updated
This flow shows how user profile data is checked, created if missing, loaded, edited, and saved in Firebase Firestore.
Execution Sample
Firebase
const userId = auth.currentUser.uid;
const profileRef = firestore.collection('profiles').doc(userId);
const doc = await profileRef.get();
if (!doc.exists) {
  await profileRef.set({name: '', email: ''});
}
This code checks if a user profile exists in Firestore; if not, it creates a new empty profile.
Process Table
StepActionConditionResultFirestore State
1Get current user IDN/AuserId = 'abc123'No change
2Reference profile documentN/AprofileRef points to 'profiles/abc123'No change
3Get profile documentN/Adoc fetchedNo change
4Check if doc existsdoc.exists == falseProfile does not existNo change
5Create new profileN/ASet {name:'', email:''} in FirestoreNew profile created at 'profiles/abc123'
6EndN/AProfile ready for useProfile document exists
💡 Profile created because it did not exist before
Status Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
userIdundefined'abc123''abc123''abc123''abc123'
doc.existsundefinedundefinedfalsefalsefalse
profileRefundefinedundefinedref to 'profiles/abc123'ref to 'profiles/abc123'ref to 'profiles/abc123'
Key Moments - 2 Insights
Why do we check if the profile document exists before creating it?
Because creating a profile without checking could overwrite existing data. See execution_table step 4 where doc.exists is false, so we create the profile only then.
What happens if the profile already exists?
The code skips creating a new profile and uses the existing data. This is implied by the condition in step 4; if doc.exists was true, creation would be skipped.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of doc.exists?
Atrue
Bundefined
Cfalse
Dnull
💡 Hint
Check the 'Condition' column in step 4 of execution_table
At which step is the new profile created in Firestore?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table
If the profile document already existed, which step would be skipped?
AStep 5
BStep 3
CStep 2
DStep 6
💡 Hint
Refer to the condition in step 4 and what happens next in execution_table
Concept Snapshot
User profile data in Firebase Firestore:
- Check if profile document exists using get()
- If not exists, create with set()
- Use user ID as document key
- Load and update profile data as needed
- Always check existence to avoid overwriting
Full Transcript
This visual execution shows how Firebase Firestore handles user profile data. First, the app gets the current user's ID. Then it references the profile document in the 'profiles' collection using that ID. It fetches the document to check if it exists. If the profile does not exist, the app creates a new profile document with empty fields. This prevents overwriting existing profiles. Finally, the profile is ready for use or editing. Variables like userId and doc.exists change as the code runs. Key moments include why existence is checked and what happens if the profile exists. The quiz tests understanding of these steps.