I am trying to verify the uniqueness of a username when registering a user. I know that there are many questions about this, and I went through all of them, but my problem is that it does not do anything, it does not print that the username exists and does not even register with the username.
What am I doing wrong? It seems that I can not find the answer, the only thing that may be wrong is the nesting enclosure.
This is a struct:
AppName:
users
1v2mRJTvrBQ7dMQohUU3rnn7ypI3: //UID
username: John
And this 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!")
}
}
}
})
}else{
print("Username already exists")
}
})
}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 they should not have any meaning except the ones I used:
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"Snuses": {
".indexOn": "Brand",
"$username": {
".validate": "!root.child(users').hasChild($username)"
}
}
}
}
What am I wrong about?
source
share