您只能将电子邮件和密码传递给createUserWithEmailAndPassword(),因此无法使用此功能设置名称。
如果你想为认证用户设置一个名字,你需要在用户对象上使用updateProfile()方法,像这样:
user.updateProfile({
displayName: "Jon Snow"
})
请注意,该属性称为displayName,而不是name。
您可以在创建用户后立即设置名称。此示例使用 async/await:
const { user } = await auth.createUserWithEmailAndPassword(
email,
password
)
await user.updateProfile({ displayName: name });
或者,使用承诺:
auth.createUserWithEmailAndPassword(email, password)
.then(({ user }) => user.updateProfile({ displayName: name }))
.then(() => {
console.log("User was updated!", auth.currentUser.displayName);
});
如果您在代码中的其他位置需要用户对象,可以调用firebase.auth().currentUser 或attach a listener to the authentication object。
您只能为 Firebase 用户设置有限数量的属性(例如 displayName、email 和 photoURL)。附加(自定义)数据必须存储在其他地方,例如在 Firestore 中,连同用户的 uid。