0

我希望能夠檢查電子郵件地址是否已被使用(所以如果有人把[email protected],但另一個用戶已經註冊了該電子郵件帳戶)。Swift Firebase檢查電子郵件是否已被使用

我有一個簡單的測試,如果它沒有被使用的圖像視圖示出了綠色箭頭,如果已經使用它,然後它是紅色的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") 
     } 

    }); 

} 

到目前爲止,這是行不通的,因爲我可以在我的數據庫[email protected]存在看。 有人能告訴我我錯過了什麼嗎?

編輯

我已經更新了我的代碼。我使用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: "[email protected] 
+0

當您針對Firebase數據庫執行查詢時,可能會有多個結果。所以快照包含了這些結果的列表。即使只有一個結果,快照也會包含一個結果列表。如果沒有結果,你會得到一個空的列表。你會想檢查'snapshot.hasChildren()'。請參閱https://firebase.google.com/docs/reference/ios/firebasedatabase/api/reference/Classes/FIRDataSnapshot#-haschildren –

+0

@FrankvanPuffelen我讀了那個文檔,看到了snapshot.hasChild(String)...當我使用,並在電子郵件中輸入我得到一個崩潰說(hasChild :)必須是一個非空字符串,不包含'。' '''''''''''''''' – RubberDucky4444

+0

我連接到'hasChildren()'(複數),而不是'hasChild()'(它需要一個字符串參數來指示要檢查的孩子)。 –

回答

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

} 
+0

我已經嘗試過與其他SO答案結合使用,但我仍然無法使其工作。 – RubberDucky4444

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

如果電子郵件地址已註冊用戶,您將獲得的列表提供商的電子郵件地址用於。否則,提供者列表將爲空,因此電子郵件地址未被註冊。

相關問題