Firebase username uniqueness in Swift

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!")
                            }
                        }
                        //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 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?

-1
source share
3

. , . , , (auth!= Null).

, , " ". . , , , . - "".

+1

Firebase, ", - ", , . , , , .

if errMessage != nil{

guard let uid = user?.uid else{

else, , S.O. :

if snapshot.value is NSNull{

, Util.errorAlert .

0

Firebase (), .

FIRAuth.auth()?.createUser(withEmail: email, password: password) { (user, error) in
  // ...
}

4 , :

FIRAuthErrorCodeInvalidEmail
     Indicates the email address is malformed.

FIRAuthErrorCodeEmailAlreadyInUse  (this is the one to address you question)
     Indicates the email used to attempt sign up already exists. Call 
     fetchProvidersForEmail to check which sign-in mechanisms such
     user used, and prompt the user to sign in with one of those.

FIRAuthErrorCodeOperationNotAllowed
     Indicates that email and password accounts are not enabled. Enable them in the
     Authentication section of the Firebase console.

FIRAuthErrorCodeWeakPassword
     Indicates an attempt to set a password that is considered too weak. The 
     NSLocalizedFailureReasonErrorKey field in the NSError.userInfo
     dictionary object will contain more detailed explanation that can
     be shown to the user.
0

All Articles