React-Native Firebase注册如何获取用户名

时间:2019-02-02 17:23:44

标签: firebase react-native

这是我的注册码;

onButtonClicked1() {
  this.setState({
    error: '',
    loading: true
  })
  const {username, password} = this.state;
  firebase.auth().createUserWithEmailAndPassword(username, password)
  .then(this.onRegisterSuccess.bind(this))
  .catch(() => {
    this.setState({
      error : 'Not registered.',
      loading: false
    })
  });
}

如何获取用户名? 取得用户名后,就可以在设置页面中对其进行更新。

1 个答案:

答案 0 :(得分:0)

在解决承诺后(将来可能会改变),createUserWithEmailAndPassword firebase方法将返回具有此结构的Object:

{
  additionalUserInfo: Object,
  credential: null,
  operationType: "signIn",
  user: {
    displayName: string,
    email: string,
    emailVerified: string,
    uid: string,
    ...
  }
}

从那里您可以访问所有可用信息。但是,请记住,createUserWithEmailAndPassword期待emailpassword。因此,如果您应用中的username是电子邮件,那一切都很好。如果没有,您可能无法获得所需的结果。

示例的使用承诺后的功能被解决:

firebase.auth().createUserWithEmailAndPassword(username, password)
  .then((res) => {
    callAnyFunctionYouWant(res.user.email) // or res.user.displayName
  })
  .catch(() => {
    ...
  });

如果您确实想要用户名

如果您要使用自定义username,则需要在firebase.auth()之后为用户创建一个配置文件,并使用其他查询来获取它。那样,您将无法使用提供的示例代码。它需要额外的工作。

希望可以解决所有问题。