如何检查电子邮件地址是否已被使用Firebase [英] How To Check If An Email Address Is Already In Use Firebase

查看:250
本文介绍了如何检查电子邮件地址是否已被使用Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理一些 FirAuth 的东西,但由于某种原因,我不知道如何检查用户提出的电子邮件地址是否已被采取。我已经尝试调用 .fetchProvidersForEmail ,正如前面提到的其他问题一样,但是无论出于什么原因,它都不起作用。而且,对于完成处理程序,我也非常青睐,所以任何有建设性的批评都会受到欢迎。所以,现在,我的代码与此有关的部分是:
$ b $ pre $ 导入UIKit
导入Firebase
class LoginViewController:UIViewController {
var reply:Bool = true
@IBOutlet weak var emailTxt:UITextField!

@IBAction func nextScreen(sender:UIButton){
print(emailCheck(emailTxt.text!))
}

func emailCheck(input: (输入,完成:{
结果,
中的错误var decision:Bool = true
){
let pullEmails = FIRAuth.auth()!. fetchProvidersForEmail如果error ==nil{
print(error)
self.reply = true
} else {
print(error)
self.reply = false

})
return


$ c


$ b

所以发生这种情况的答案总是打印 true ,可能是因为我明确地将其定义在顶部。无论哪种方式,就好像完成处理程序不会在返回值之前等待实际完成。我试图找出如何使用它们,以及 AsyncTasks ,但我想知道是否有一个简单的错误,我在这里可以解决这个问题。谢谢大家!

解决方案

这个答案花了我很长时间才发现,但是在这里。

  func emailCheck(输入:String,回调:(isValid:Bool) - >无效){
FIRAuth。 auth()?。signInWithEmail(input,password:){(user,error)in
var canRegister = false

if error!= nil {
if(error ?.code == 17009){
canRegister = false
} else if(error?.code == 17011){
//电子邮件不存在
canRegister = true


$ b callback(isValid:canRegister)
}
}

这段代码的工作原理非常简单。本质上,如果您将非空密码传递到Firebase,即而不是,您将得到某种错误代码回来。我正在寻找的两个最重要的返回码是 17009 17011 。这是因为如果返回 17009 (表示密码错误),那么我们知道已经有一个电子邮件地址与建议的地址相关联,所以用户不应该能够注册一个新的。下一个代码 17011 非常简单。这意味着没有给定的电子邮件地址的文件的用户,这十个允许用户要求它。我使用这个函数调用这个函数:

pre $ mailCheck(emailTxt.text!){isValid in $ b $ if ifValid {
print(valid)
} else {
print(Invalid!)
}

然后管理来自它的 boolean 。希望这可以帮助其他人在相同的情况!

working on some FirAuth things, but for some reason I can't figure out how to check if the users proposed email address has already been taken. I've tried calling .fetchProvidersForEmail as suggested in other questions from a while back, but for whatever reason it just won't work. Also, I'm very green when it comes to completion handlers, so any constructive criticism on that would be welcome as well. So, for now, the parts of my code that pertain to this are:

import UIKit
import Firebase
class LoginViewController: UIViewController {
var reply : Bool = true
@IBOutlet weak var emailTxt: UITextField!

@IBAction func nextScreen(sender: UIButton) {
print(emailCheck(emailTxt.text!))
}

 func emailCheck(input : String)->Bool{
    let pullEmails = FIRAuth.auth()!.fetchProvidersForEmail(input, completion:{
        result,error in
        var decision : Bool = true
        if error == "nil" {
            print(error)
            self.reply = true
        }else{
            print(error)
            self.reply = false
        }
    })
    return reply
}
}

So what happens with this is that reply always prints true, probably because I explicitly defined it to at the top. Either way, it is almost as if the completion handler isn't waiting to actually be completed before it returns the value. I've tried to figure out how to use them, as well as AsyncTasks, but I was wondering if there was a simple error I've made in here that could solve this. Thanks everybody!

解决方案

This answer took my quite a long time to find, but here it is.

func emailCheck(input: String, callback: (isValid: Bool) -> Void) {
    FIRAuth.auth()?.signInWithEmail(input, password: " ") { (user, error) in
        var canRegister = false

        if error != nil {
            if (error?.code == 17009) {
                canRegister = false
            } else if(error?.code == 17011) {
                //email doesn't exist
                canRegister = true
            }
        }

        callback(isValid: canRegister)
    }
}

The reason why this code works is pretty simple. Essentially if you pass a non null password into Firebase, i.e. " " instead of "" you will get some sort of error code back. The two most important return codes that I am looking for is 17009 and 17011. This is because if 17009 is returned (signifying a wrong password) then we know that there already is an email address associated with that proposed address, so therefore the user shouldn't be able to sign up for a new one. The next code, 17011 is pretty straight forward. It means that there is no user on file with that given email address, which ten allows the user to claim it. I call this function by using this:

mailCheck(emailTxt.text!) { isValid in
                if isValid {
                    print("valid")
                } else {
                    print("Invalid!")
                }

And then manage the boolean that comes from it. Hope this helps other people in the same situation!

这篇关于如何检查电子邮件地址是否已被使用Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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