0
0
Firebasecloud~5 mins

User profile data in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
User profile data stores personal information about users in your app. It helps you keep track of user details like name, email, and preferences securely and accessibly.
When you want to save a user's name and email after they sign up.
When you need to update user preferences like theme or notification settings.
When you want to display user information on their profile page.
When you want to keep user data synced across devices automatically.
When you want to secure user data with Firebase's built-in rules.
Config File - firestore.rules
firestore.rules
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
  }
}

This security rule file controls access to user profile data in Firestore.

The match /users/{userId} block means rules apply to documents in the 'users' collection.

It allows read and write only if the user is signed in and their user ID matches the document ID, protecting user data privacy.

Commands
Initializes Firestore in your Firebase project to store user profile data.
Terminal
firebase init firestore
Expected OutputExpected
=== Firestore Setup === Firestore rules and indexes files have been created. ✔ Firestore initialization complete.
Deploys Firestore rules and indexes to Firebase to enforce security on user profile data.
Terminal
firebase deploy --only firestore
Expected OutputExpected
=== Deploying to 'your-project-id'... ✔ Deploy complete! Project Console: https://console.firebase.google.com/project/your-project-id/overview
--only firestore - Deploys only Firestore related files, avoiding other services.
Creates a user profile document with ID 'user123' containing name and email fields.
Terminal
firebase firestore:documents create users/user123 --data '{"name":"Alice","email":"alice@example.com"}'
Expected OutputExpected
Document users/user123 created successfully.
Retrieves the user profile data for user with ID 'user123' to verify it was saved correctly.
Terminal
firebase firestore:documents get users/user123
Expected OutputExpected
{ "name": "Alice", "email": "alice@example.com" }
Key Concept

If you remember nothing else from this pattern, remember: secure user profile data by matching document IDs to authenticated user IDs in Firestore rules.

Common Mistakes
Allowing open read/write access to all user profile documents.
This exposes all user data to anyone, risking privacy and security breaches.
Use Firestore rules to restrict access so users can only read and write their own profile documents.
Using different document IDs than the user's authentication ID.
It makes it hard to enforce security rules that rely on matching user IDs, causing access errors.
Use the authenticated user's UID as the document ID for their profile data.
Summary
Initialize Firestore in your Firebase project to store user profile data.
Write and deploy Firestore security rules to restrict access to each user's own data.
Create and retrieve user profile documents using the user's authentication ID as the document ID.