首页 文章

如何检查Firebase中是否存在用户?

提问于
浏览
0

我终于使用我的身份验证来创建用户和登录和注销 . 但现在,我想实现一些检查用户是否已存在于Firebase中的内容 . 我查了一下,但似乎找不到具体的答案 .

例如,如果我的电子邮件地址是:abc12@gmail.com而其他人尝试使用相同的电子邮件地址注册,我该如何告诉他们已经使用了?

login(e) {
    e.preventDefault();

    fire.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
        .then((u) => {
        }).catch((error) => {
        console.log(error);
    });
}

signup(e) {
    e.preventDefault();

    fire.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
        .then((u) => {
        }).catch((error) => {
        console.log(error);
    });
}

1 回答

  • 2

    从方法 createUserWithEmailAndPassword 返回的错误具有 code 属性 . 根据documentation错误 code auth/email-already-in-use

    如果已存在具有给定电子邮件地址的帐户,则抛出该异常 .

    您至少可以使用 if / elseswitch 等条件语句来检查 code 并向用户显示/ log / dispatch / etc消息或代码:

    fire.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
      .then(u => {})
      .catch(error => {
         switch (error.code) {
            case 'auth/email-already-in-use':
              console.log(`Email address ${this.state.email} already in use.`);
            case 'auth/invalid-email':
              console.log(`Email address ${this.state.email} is invalid.`);
            case 'auth/operation-not-allowed':
              console.log(`Error during sign up.`);
            case 'auth/weak-password':
              console.log('Password is not strong enough. Add additional characters including special characters and numbers.');
            default:
              console.log(error.message);
          }
      });
    

    希望这有帮助!

相关问题