Swift Firebase检查电子邮件是否已被使用 [英] Swift Firebase check if email is already in use
问题描述
我有一个简单的测试,如果它没有被使用过一个图像视图显示一个绿色的箭头,如果它已经被使用,那么它是红色的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 {
//注册失败
}
我正在尝试检查是
pre $ func checkIfEmailExists(textField:UITextField){
let ref = FIRDatabase.database()。参考()
让email = firstContainerTextField.text
ref.child(userEmails)。queryEqual(toValue:email)
.observe(.value,with:{snapshot in
$ b $ 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(命名为:greenCheck.png)
}
});
$ p
$ b
到目前为止, 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(命名为:redEx.png)
} else {
为快照.children.allObjects中的子节点![FIRDataSnapshot] {
让tmp = child.value as!字符串
如果tmp == email {
self.firstContainerImage.image = UIImage.init(命名为:greenCheck.png)
}
}
}
});
编辑2
我改变了我设置用户的方式
self.ref.child(users) .child((user?.uid)!)。setValue([Email:email])
所以现在我的数据库看起来像这样
users
***** uid *****
电子邮件:test@test.com
我之前评论过:你需要通过调用 snapshot.hasChildren()
来检查查询是否有结果。
< pre $ func checkIfEmailExists(textField:UITextField){
let ref = FIRDatabase.database()。reference()
let email = firstContainerTextField.text
ref.child(userEmails)。queryEqual(toValue:email)
.observe(.value,其中:{snapshot in
if (!snapshot.hasChildren()){
//用户还不存在...
}
} );
}
I want to be able to check if an email address is already been used (so if somebody put test1@test.com but another user already registered with that email account).
I have a simple test if it has NOT been used an image view shows a green arrow, if it HAS been used then it is red x
when I create the user I use the following code
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
}
what I am trying to do to check is
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")
}
});
}
So far it does not work as I can see in my database that test1@test.com exists. Can somebody tell me what I missed?
EDIT
I have updated my code. I am using hasChildren and I searched for similar questions and they seem to point this direction, but I still cannot get the result I am looking for
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")
}
}
}
});
}
Edit 2
I changed how I set my user up
self.ref.child("users").child((user?.uid)!).setValue(["Email": email])
so now my database looks like this
users
*****uid*****
Email: "test@test.com
As I commented earlier: you'll need to check whether the query has any results by calling 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...
}
});
}
这篇关于Swift Firebase检查电子邮件是否已被使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!