简体   繁体   English

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

[英]How can I check if user exists in 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.但是现在,我想实现一些检查用户是否已经存在于 Firebase 的东西。 I've looked it up but can't seem to find a concrete answer.我查了一下,但似乎找不到具体的答案。

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?例如,如果我的电子邮件地址是: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);
    });
}

The error that is returned from method createUserWithEmailAndPassword has a code property.从方法createUserWithEmailAndPassword返回的错误具有code属性。 Per the documentation the error code auth/email-already-in-use :根据文档,错误code auth/email-already-in-use

Thrown if there already exists an account with the given email address.如果已存在具有给定电子邮件地址的帐户,则抛出。

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:至少,您可以使用条件语句,例如if / elseswitch来检查该code并向用户显示/记录/调度/等消息或代码:

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

Hopefully that helps!希望这有帮助!

更简单的答案:

 const uidExists = auth().getUser(uid).then(() => true).catch(() => false))
 const emailExists = auth().getUserByEmail(email).then(() => true).catch(() => false))

from firebase_admin import auth

user = auth.get_user_by_email(email)
print('Successfully fetched user data exists: {0}'.format(user.uid))

In python admin server在 python 管理服务器中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM