简体   繁体   English

Firebase Auth和Swift:检查数据库中是否已存在电子邮件

[英]Firebase Auth and Swift: Check if email already in database

I'm working on an iOS app which will use Firebase for user management (sign up, sign in, etc.) 我正在使用iOS应用,该应用将使用Firebase进行用户管理(注册,登录等)。

I'm new to Firebase, but it's mostly going ok. 我是Firebase的新手,但是一切正常。 I've connected it, I have created users and logged in, etc. 我已经连接好了,已经创建了用户并登录,等等。

But, I'm trying to change my UI so that the "Sign up" button is initially hidden and will only appear when: 但是,我正在尝试更改UI,以使“注册”按钮最初处于隐藏状态,并且仅在以下情况下显示:

  1. all fields are not empty 所有字段都不为空
  2. email address is valid (using regex) 电子邮件地址有效(使用正则表达式)
  3. email address in not already in the database 数据库中尚未存在的电子邮件地址
  4. user name is not already in the database 用户名尚未在数据库中
  5. password and confirmPassword fields are equal 密码和ConfirmPassword字段相等

I can't figure out #3 and #4. 我不知道#3和#4。

I've been reading documentation, watching videos, chasing links all over StackO and beyond, but I can't figure it out. 我一直在阅读文档,观看视频,在StackO以及其他地方追踪链接,但我不知道。

Can anyone point me in the right direction? 谁能指出我正确的方向?

If you are using email & password authentication, the solution is very simple. 如果您正在使用电子邮件和密码身份验证,则解决方案非常简单。

Firebase Authentication will not allow duplicate emails so when the createUser function is executed, if the email already exists Firebase will return a emailAlreadyInUse error in the error parameter. Firebase身份验证将不允许重复的电子邮件,因此,当执行createUser函数时,如果电子邮件已经存在,则Firebase会在error参数中返回emailAlreadyInUse错误。 You can then cast this to an NSError to see which one it is and handle appropriately. 然后可以将其强制转换为NSError,以查看它是哪一个并进行适当处理。

So the function is like this 所以功能是这样的

Auth.auth().createUser(withEmail: createEmail, password: password ) { user, error in
   if let x = error {
      let err = x as NSError
      switch err.code {
      case AuthErrorCode.wrongPassword.rawValue:
         print("wrong password")
      case AuthErrorCode.invalidEmail.rawValue:
         print("invalid email")
      case AuthErrorCode.accountExistsWithDifferentCredential.rawValue:
         print("accountExistsWithDifferentCredential")
      case AuthErrorCode.emailAlreadyInUse.rawValue: //<- Your Error
         print("email is alreay in use")
      default:
         print("unknown error: \(err.localizedDescription)")
      }
      //return
   } else {
      //continue to app
   }

I threw some random errors into that case statement but check the link for a complete list of all AuthErrorCodes. 我在该case语句中添加了一些随机错误,但请检查链接以获取所有AuthErrorCodes的完整列表。

You can also do this 您也可以这样做

Auth.auth().fetchSignInMethods(forEmail: user, completion: { (signInMethods, error) in
    print(signInMethods)
})

I think you can check it by using this method 我认为您可以使用此方法进行检查

   let ref1 = Database.database().reference().child("Users").queryOrdered(byChild: "UserName").queryEqual(toValue: "UserName enter by user")

        ref1.observeSingleEvent(of: .value) { (sanpshot) in

            print(sanpshot.exists()) // it will return true or false
        }

and same for email. 与电子邮件相同。

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

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