You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Firebase中为用户添加自定义个人资料字段?

Adding Custom User Fields (Age, Gender, Country) in Firebase

Great question! Firebase Auth's user.updateProfile() method is limited to updating the built-in displayName and photoURL properties, but adding custom user data like age, gender, or country is absolutely feasible. Here are the two most practical approaches:

Firestore is Firebase's flexible NoSQL database, perfect for storing user-specific custom fields. You’ll create a collection (e.g., users) where each document is tied to a user’s unique Firebase Auth UID.

Example Code

First update the default profile fields if needed, then save the custom data to Firestore:

// Update built-in profile fields first
user.updateProfile({
  displayName: "Jane Q. User",
  photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(async function() {
  // Save custom fields to Firestore
  const db = firebase.firestore();
  await db.collection('users').doc(user.uid).set({
    age: 28,
    gender: "female",
    country: "Canada",
    lastUpdated: firebase.firestore.FieldValue.serverTimestamp()
  }, { merge: true }); // Use merge to avoid overwriting existing data
  
  console.log("Profile and custom data updated successfully!");
}).catch(function(error) {
  console.error("Error updating profile or custom data:", error);
});

Key Notes

  • Security Rules: Ensure only the authenticated user can modify their own document with these 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;
        }
      }
    }
    
  • Fetching Data: Retrieve custom data later by fetching the user’s document:
    const userDoc = await db.collection('users').doc(user.uid).get();
    if (userDoc.exists) {
      const customUserData = userDoc.data();
      console.log("User age:", customUserData.age);
    }
    

2. Use Firebase Realtime Database (Alternative)

If you prefer Realtime Database over Firestore, the approach is similar—create a users node and store custom fields under the user’s UID:

Example Code

user.updateProfile({
  displayName: "Jane Q. User",
  photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(function() {
  const db = firebase.database();
  db.ref('users/' + user.uid).update({
    age: 28,
    gender: "female",
    country: "Canada"
  }).then(() => {
    console.log("All data updated!");
  });
}).catch(function(error) {
  console.error("Error:", error);
});

When to Avoid Custom Claims

Firebase Auth has a feature called custom claims, but it’s designed for small, permission-related data (e.g., isAdmin: true), not user profile details like age or country. Custom claims have size limits and are meant for authorization, not general user data storage.


内容的提问来源于stack exchange,提问作者Don Rajitha Dissanayake

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:33:31