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

如何在firebase-auth中添加额外字段以存储用户全名等自定义信息

完全可以在Firebase中存储用户全名,通常有两种成熟实现方案,可根据作业需求选择:

方案1:直接存储到Firebase Auth用户的displayName内置字段

该方案无需额外开通数据库服务,直接使用Auth系统自带的用户属性存储,实现逻辑最轻量。
实现逻辑:

  • 先调用createUserWithEmailAndPassword接口完成邮箱+密码的基础注册逻辑
  • 注册成功后获取当前用户实例,调用updateProfile方法传入用户全名参数即可

Web端代码示例:

import { getAuth, createUserWithEmailAndPassword, updateProfile } from "firebase/auth";

const auth = getAuth();
// 这里的email、password、fullName都是从前端表单获取的用户输入值
createUserWithEmailAndPassword(auth, email, password)
  .then((userCredential) => {
    const user = userCredential.user;
    // 更新用户全名到Auth内置字段
    updateProfile(user, {
      displayName: fullName 
    }).then(() => {
      // 操作完成,后续可直接从user.displayName读取用户全名
    });
  })
  .catch((error) => {
    console.log(error.code, error.message);
  });

方案2:存储到Firebase Firestore数据库(扩展性更强)

如果后续作业还需要扩展存储更多用户自定义字段(比如手机号、头像、个人简介等),推荐将用户全量信息存储在独立的Firestore集合中,用用户UID作为文档ID方便关联查询。
实现逻辑:

  • 先在Firebase控制台开通Cloud Firestore数据库,配置安全规则允许用户读写自己的用户文档
  • 邮箱密码注册成功后,将用户全名、邮箱、UID等信息写入users集合的对应文档

Web端代码示例:

import { getAuth, createUserWithEmailAndPassword } from "firebase/auth";
import { getFirestore, doc, setDoc } from "firebase/firestore";

const auth = getAuth();
const db = getFirestore();
createUserWithEmailAndPassword(auth, email, password)
  .then(async (userCredential) => {
    const user = userCredential.user;
    // 写入用户全量数据到Firestore
    await setDoc(doc(db, "users", user.uid), {
      fullName: fullName,
      email: email,
      uid: user.uid
    });
  })
  .catch((error) => {
    console.log(error.code, error.message);
  });

两种方案逻辑通用,如果你使用的是Android、iOS、Flutter等其他端,只要调用对应端SDK的同名方法即可实现相同效果。

内容的提问来源于stack exchange,提问作者Abhishek kamal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 05:12:01