Swift Firebase检查电子邮件是否已被使用

时间:2016-12-29 02:27:16

标签: swift firebase firebase-realtime-database firebase-authentication

我希望能够检查是否已经使用了电子邮件地址(所以如果有人放了test1@test.com,而另一个用户已经注册了该电子邮件帐户)。

我有一个简单的测试,如果没有使用图像视图显示绿色箭头,如果它已被使用,那么它是红色x

当我创建用户时,我使用以下代码

FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in

    if error == nil {

      self.ref.child("userEmails").child((user?.uid)!).setValue(email)


      FIRAuth.auth()!.signIn(withEmail: email,
                             password: password)

    } else {
      //registration failure
    }

我要检查的是

func checkIfEmailExists(textField: UITextField) {

    let ref = FIRDatabase.database().reference()

    let email = firstContainerTextField.text ?? ""

    ref.child("userEmails").queryEqual(toValue: email)
      .observe(.value, with: { snapshot in

        if (self.firstContainerTextField.text?.isEmpty)! {
          self.firstContainerImage.image = UIImage.init(named: "emptyBlue.png")

        } else if !(self.firstContainerTextField.text?.isEmpty)! && !snapshot.exists() {
          self.firstContainerImage.image = UIImage.init(named: "redEx.png")

        } else if snapshot.exists() {
          self.firstContainerImage.image = UIImage.init(named: "greenCheck.png")
        }

    });

}

到目前为止它不起作用,因为我在我的数据库中看到test1@test.com存在。 有人能告诉我我错过了什么吗?

修改

我已更新了我的代码。我正在使用hasChildren,我搜索了类似的问题,他们似乎指出了这个方向,但我仍然无法得到我想要的结果

func checkIfEmailExists(textField: UITextField) {

let ref = FIRDatabase.database().reference()

let email = firstContainerTextField.text ?? ""

ref.child("userEmails").queryEqual(toValue: email)
  .observe(.value, with: { snapshot in

    if !snapshot.hasChildren() {

      self.firstContainerImage.image = UIImage.init(named: "redEx.png")

    } else {

      for child in snapshot.children.allObjects as! [FIRDataSnapshot] {

        let tmp = child.value as! String


        if tmp == email {

          self.firstContainerImage.image = UIImage.init(named: "greenCheck.png")

        }

      }
    }

  });
}

修改2

我改变了设置用户的方式

self.ref.child("users").child((user?.uid)!).setValue(["Email": email])

所以现在我的数据库看起来像这样

users
    *****uid*****
        Email: "test@test.com

2 个答案:

答案 0 :(得分:0)

正如我之前评论的那样:您需要通过调用snapshot.hasChildren()来检查查询是否有任何结果。

func checkIfEmailExists(textField: UITextField) {

    let ref = FIRDatabase.database().reference()

    let email = firstContainerTextField.text ?? ""

    ref.child("userEmails").queryEqual(toValue: email)
      .observe(.value, with: { snapshot in
        if (!snapshot.hasChildren()) {
            // User doesn't exist yet...
        }
    });

}

答案 1 :(得分:0)

以下是您可能正在寻找的Firebase function的结构(Swift 4):

Auth.auth().fetchProviders(forEmail: emailAddress, completion: {
        (providers, error) in

        if let error = error {
            print(error.localizedDescription)
        } else if let providers = providers {
            print(providers)
        }
    })

如果电子邮件地址已经注册到用户,您将获得该电子邮件地址用于的提供商列表。否则,提供者列表将为空,因此电子邮件地址未注册。

相关问题