本文介绍了Firebase在Swift中的用户名唯一性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我试图在注册用户时检查用户名的唯一性。我知道有很多关于它的问题,我经历了所有的问题,但是我的问题是它什么都不做,它不打印用户名存在,甚至不注册用户名。 p>

我做错了什么?我似乎无法找到答案,唯一可能是错误的是如果嵌套。



这是结构:

 应用程序名称:
用户
1v2mRJTvrBQ7dMQohUU3rnn7ypI3:// UID
用户名:John

这就是代码:

  func registerUser ){
guard let username = usernameField.text,让email = emailField.text,让password = passwordField.text else {
print(成功注册)
return
}
if connectedToNetwork()== true {
if passwordField.text == confirmPasswordField.text {
let usersRef = FIRDatabase.database()。reference()。child(users)$ queryEqual(toValue:\(username))
.observeSingleEvent(of:FIRDataEventType.value,with:{(snapshot)in
如果snapshot.value是NSNull {

AuthProvider.Instance.register(withEmail:email,password:password,username:username,loginHandler:{(errMessage)in
if errMessage!= nil {

} else {
let user = FIRAuth.auth()?。currentUser
guard let uid = user?.uid else {
return
}
user?.sendEmailVerification(){
中的错误如果发生错误=错误{
print(error.localizedDescription)
} else {
print(Email has been sent to you! )
}
}
//用户登录并重定向

}
})
} else {
prin t(用户名已存在)//不执行这个


$ else b $ b $ Util.errorAlert(消息:密码不匹配! )
}
} else {
Util.errorAlert(消息:互联网连接似乎处于脱机状态。)
}
}

我甚至尝试了不同的安全规则,但是这些应该没有什么区别,但是我使用的是:
$ b $

  {
rules:{
.read:auth!= null,
写入:auth!= null,
Snuses:{
.indexOn:Brand,
$ username:{
.validate :!root.child(users')。hasChild($ username)



$ b code pre>

我做错了什么?

解决方案

您的问题在于您的安全规则。用户进行身份验证之前,您正在执行查询。此查询将失败,因为您的规则声明用户必须通过身份验证才能查询数据库(auth!= null)。



您需要做的是创建另一个孩子叫用户名。让任何人都可以阅读,而不需要验证检查用户名是否被采用,如果没有,请为新用户声明。然后,进行身份验证调用,并在用户子中创建用户。


I am trying to check username uniqueness while registering user. I know there are tons of questions about it and I went through all of them however my problem is that it doesn't do anything, it doesn't print that the username exists and it even doesn't register with the username.

What I am doing wrong? I can't seem to find answer, the only thing that could be wrong is the if nesting.

This is the struct:

AppName:
    users
     1v2mRJTvrBQ7dMQohUU3rnn7ypI3: //UID
     username: John

And that is the code:

func registerUser(){
        guard let username = usernameField.text, let email = emailField.text, let password = passwordField.text else{
            print("Successfully registered")
            return
        }
        if connectedToNetwork() == true{
            if passwordField.text == confirmPasswordField.text{
                let usersRef = FIRDatabase.database().reference().child("users")
                usersRef.queryOrdered(byChild: "username").queryEqual(toValue: "\(username)")
                .observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in
                    if snapshot.value is NSNull{

                AuthProvider.Instance.register(withEmail: email, password: password, username: username, loginHandler: { (errMessage) in
                    if errMessage != nil{

                    }else{
                        let user = FIRAuth.auth()?.currentUser
                        guard let uid = user?.uid else{
                            return
                        }
                        user?.sendEmailVerification() { error in
                            if let error = error {
                                print(error.localizedDescription)
                            } else {
                                print("Email has been sent to you!")
                            }
                        }
                        //User logged in and redirect

                    }
                })
                    }else{
                    print("Username already exists")// It doesn't execute this
                    }
                })
            }else{
                Util.errorAlert(message: "Passwords do not match!")
            }
        }else{
            Util.errorAlert(message: "The internet connection appears to be offline.")
        }
    }

I even tried different security rules but those shouldn't make any difference but those that I used:

{
"rules": {
".read": "auth != null",
".write": "auth != null",
"Snuses": {
".indexOn": "Brand",
    "$username": {
        ".validate": "!root.child(users').hasChild($username)"
    }
}
}
}

What is the thing I am doing wrong?

解决方案

Your problem is in your security rules. You're doing a query before the user has been authenticated. This query will fail because your rules state that the user has to be authenticated in order to query the database (auth != null).

What you need to do is create another child called "usernames". Make this readable by anyone without authentication. Check if the username is taken and if not, claim it for the new user. Then, do your auth call and create your user in the "users" child.

这篇关于Firebase在Swift中的用户名唯一性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:20