【问题标题】:Firebase Authentication using custom fields使用自定义字段的 Firebase 身份验证
【发布时间】:2020-06-02 20:35:02
【问题描述】:

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

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

向我建议的解决方法是在创建用户名后获取用户名。但我希望在创建帐户时将数据与电子邮件一起发送。有没有一种方法可以将用户名连同这个创建用户请求一起发送,这样我就可以在登录时获取用户名。提前致谢。

【问题讨论】:

    标签: html firebase firebase-authentication customization


    【解决方案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。

    【讨论】:

      猜你喜欢
      • 2020-04-01
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 2022-06-13
      相关资源
      最近更新 更多