【问题标题】:React + Firebase Form Validation if user does not exist如果用户不存在,则 React + Firebase 表单验证
【发布时间】:2020-05-07 18:48:09
【问题描述】:

目前我正在开发一种登录模式,它将使用 firebase 功能对用户进行身份验证。如果它们不存在,我计划使用 reactstrap 库将这些框变为红色并发送一条消息,例如“无效的电子邮件或密码”。但是,我是 React 的新手,当我在电子邮件表单中调用 onAuthenticate={firebase.authenticateUser} 时,我在从 firebase 函数获取返回对象/数字时遇到了很多麻烦。如何获取返回对象(如果用户不存在,则为 -1),检查它,如果用户不存在,则关闭模式或使用 react strap 将其变为红色?

我的 firebase 函数的片段,后面是我的 React 模态。

'''

exports.authenticateUser = async (email, password) => {
  firebase.auth().signInWithEmailAndPassword(email, password)
    .then(() => {
      const success = {
        success: true,
      };
      alert('User exists.');
      console.log('user exists');
      return success;
    })
    .catch((error) => {

      alert('Invalid email or password.');
      console.log('no user');
      return -1;
    });
};

return (
  <Modal
    title="Please enter log-in information"
    onClose={onClose}
    footer={modalFooter}
    titleBackgroundColor="#FFFACD"
  >
  <FormGroup>
    <Label for="examplePassword">Invalid input</Label>
    <Input invalid />
    <FormFeedback tooltip>Oh noes! that name is already taken</FormFeedback>
    <FormText>Example help text that remains unchanged.</FormText>
  </FormGroup>
    <EmailForm
      onAuthenticate={firebase.authenticateUser}
      const returnObj = {firebase.authenticateUser}
    />
  </Modal>
);

'''

【问题讨论】:

  • 在发布此问题之前,您是否尝试过查看文档或类似问题?

标签: reactjs firebase reactstrap


【解决方案1】:

你的用户应该是从 promise 返回的值。尝试以下操作,如果您的用户已登录,它应该记录您的用户,如果出现问题,它应该记录错误。

firebase.auth().signInWithEmailAndPassword(email, password)
    .then(user => console.log(user))
    .catch(error => console.log(error))

如果用户不存在且没有错误,此方法将创建用户。

有点不清楚,这是否解决了您的问题,或者您是否正在寻找一种方法来验证这是否是用户的第一次登录?由于用户在您的数据库中不存在?

如果这是您想要做的,那么注册方法将无济于事。请参考Firebase Auth, how to know new user signed up, rather than existing user sign in?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 2020-09-02
    • 2018-01-11
    • 2014-08-17
    • 2021-08-17
    • 2020-10-25
    • 1970-01-01
    相关资源
    最近更新 更多