使用自定义字段进行Firebase身份验证

时间:2020-06-02 20:35:02

标签: html firebase firebase-authentication customization

我一直在使用Firebase身份验证过程,使用电子邮件和密码。这工作得很好。但是我想在创建帐户时添加用户名。但是使用此auth函数,我只能发送两个参数,

const promise = auth.createUserWithEmailAndPassword(name.value, email.value, password.value);

向我建议的解决方法是在创建用户名后获取用户名。但是我希望在创建帐户时将数据与电子邮件一起发送。有没有一种方法可以与创建用户请求一起发送用户名,以便每次登录时都可以获取用户名。在此先感谢。

1 个答案:

答案 0 :(得分:1)

您只能将电子邮件和密码传递给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().currentUserattach a listener to the authentication object

您只能在Firebase用户上设置有限数量的属性(例如displayNameemailphotoURL)。其他(自定义)数据必须存储在其他位置,例如在Firestore中,以及用户的uid。

相关问题