在Firebase和Swift 3中进行注册之前,如何验证用户名是否存在? [英] How do I validate if a username exists before sign up in Firebase and Swift 3?

查看:63
本文介绍了在Firebase和Swift 3中进行注册之前,如何验证用户名是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我结合了五个视图控制器的注册流程,而不是一个用于Firebase注册的中央VC.

I've incorporated a sign-up flow of five view controllers as opposed to one central VC for Firebase sign up.

通常会有在注册之前丢失数据的问题,但是我会通过编程将所有输入的值通过segue推送到最终确认页面,并最终将数据发送到Firebase进行身份验证.

Normally there would be the problem of data being lost before sign up, but I'm pushing all the entered values via segue programmatically to the final confirmation page and sending the data to Firebase for auth eventually.

我的问题出在这里-我想在注册前 检查用户名是否存在.这对于我的应用程序的UX势在必行,而我不能仅用所有的VC来做到这一点.

My problem lies therein - I want to check if the username exists before signing up. This is imperative for the UX of my app and I can't just do this all one VC.

这是UsernameViewController的代码:

Here's the code I have for the UsernameViewController:

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

   rootRef.queryOrdered(byChild: "users").queryEqual(toValue: self.chooseUsernameTextField.text!)
    .observe(FIRDataEventType.value, with: { (snapshot: FIRDataSnapshot!)  in

            if snapshot.hasChildren() == false {
                print("not found")

            } else {
                print("usr found")
            }
        });

这是Firebase数据库安全规则的代码

Here's the code I have for the Firebase Database Security Rules

   {
     "rules": {
        ".read": false,
        ".write": false,
      "users": {
        ".validate": "data.child('username').val() === true",
      ".read": true
      }
     }
    }

最后是我的数据树的屏幕快照(由于某些原因,我的数据树不会让我嵌套任何新用户或创建没有值的用户节点)

and finally, a screenshot of my Data tree (My Data tree won't let me nest any new users or create a user node without a value for some reason):

Firebase数据树的图片:应用程序名称/用户

我有点怀疑我的数据规则和树已正确配置为与代码匹配,但是我陷入了XY问题,即不知道要为Firebase安全性做什么才能完成我的用户名检查代码.

I have a nagging suspicion that my data rules and tree are configured properly to match the code, but I'm stuck in a XY problem of not knowing what to do for Firebase security to get my code of username checking complete.

请帮助我!:(

推荐答案

如果Firebase的Auth部分中也创建了一个用户,那么您实际上可以使用fetchProviders方法,并且如果没有返回任何提供程序,则没有用户在您的身份验证"部分中.

If there's a user created within the Auth section of Firebase as well, then you can actually use the fetchProviders method, and if no providers are returned, you have no user in you Auth section.

FIRAuth.auth()?.fetchProviders(forEmail: email, completion: { (providers, error) in
            if providers == nil {
                   // user doesn't exist
              } else {
                   // user does exist
              }
        })

这篇关于在Firebase和Swift 3中进行注册之前,如何验证用户名是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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