如何检查Firebase中是否存在用户? [英] How can I check if user exists in Firebase?

查看:91
本文介绍了如何检查Firebase中是否存在用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于在创建用户以及登录和注销方面进行了身份验证.但是现在,我想实现一些检查用户是否已经存在于Firebase中的工具.我查了一下,但似乎找不到具体答案.

I finally got my authentication to work in terms of creating users and logging in and out. But now, I want to implement something that checks if the user already exists in Firebase. I've looked it up but can't seem to find a concrete answer.

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

For example, if my email address is: abc12@gmail.com and someone else tries to signup with the same email address, how do I tell them it's already taken?

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);
    });
}

推荐答案

从方法createUserWithEmailAndPassword返回的错误具有code属性.根据文档错误code 已在使用身份验证/电子邮件:

The error that is returned from method createUserWithEmailAndPassword has a code property. Per the documentation the error code auth/email-already-in-use:

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

Thrown if there already exists an account with the given email address.

您至少可以利用if/elseswitch之类的条件语句来检查该code并向用户显示/记录/发送/发送消息或代码:

At very minimum you can utilize conditional statements such as if/else or switch to check for that code and display/log/dispatch/etc a message or code to the user:

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.`);
          break;
        case 'auth/invalid-email':
          console.log(`Email address ${this.state.email} is invalid.`);
          break;
        case 'auth/operation-not-allowed':
          console.log(`Error during sign up.`);
          break;
        case 'auth/weak-password':
          console.log('Password is not strong enough. Add additional characters including special characters and numbers.');
          break;
        default:
          console.log(error.message);
          break;
      }
  });

希望有帮助!

这篇关于如何检查Firebase中是否存在用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆