Firebase request for unique swift username

I searched for this question but none of them had an answer that worked for me.

I want to do this, when a user registers an account, it checks if the username they have already entered exists before creating the account. I tried using queries in firebase, but I just can't seem to get it to work.

Here is a picture of what my database data looks like: My firebase data tree

How can I use a query to find the string for the key "username"?

+3


source to share


3 answers


You can go like this, do one function with a completion block to check the username already exists in your Firebase DB and create a new user based on that

func checkUserNameAlreadyExist(newUserName: String, completion: @escaping(Bool) -> Void) {

    let ref = FIRDatabase.database().reference()
    ref.child("users").queryOrdered(byChild: "username").queryEqual(toValue: newUserName)
              .observeSingleEvent(of: .value, with: {(snapshot: FIRDataSnapshot) in

        if snapshot.exists() {
            completion(true)
        }
        else {
            completion(false)
        }
    })
}

      



Now, just call this function when creating a new user:

self.checkUserNameAlreadyExist(newUserName: "Johnson") { isExist in
    if isExist {
        print("Username exist")
    }
    else {
        print("create new user")
    }
}

      

+4


source


This is how I do it:

var text = "Your username"

let dbRef = FIRDatabase.database().reference().child("users")
dbRef.queryOrdered(byChild: "name").queryEqual(toValue: text).observeSingleEvent(of: .value, with: { snapshot in
    if !snapshot.exists() {
         // Name doesn't exist
    }

    if let data = snapshot.value as? [String: [String: String]] {
         // it should exist if it reaches here
    }
})

      



Make sure your database rules index "users" node to "name" to optimize performance.

+2


source


I do it like this:

Register function:

@IBAction func signUpButtonTapped(_ sender: Any) {
  User.getItemByLogin(for: userLogin.text!,
                      completion: { userItem in
                       if userItem == nil {
                          self.createAndLogin()
                       } else {
                          self.showAlertThatLoginAlreadyExists()
                       }
  })
}


private func createAndLogin() {
  FIRAuth.auth()!.createUser(withEmail: userEmail.text!,
                             password: userPassword.text!) { user, error in
                                if error == nil {
                                   // log in
                                   FIRAuth.auth()!.signIn(withEmail: self.userEmail.text!,
                                                          password: self.userPassword.text!,
                                                          completion: { result in
                                                           // create new user in database, not in FIRAuth
                                                           User.create(with: self.userLogin.text!)

                                                           self.performSegue(withIdentifier: "fromRegistrationToTabBar", sender: self)
                                   })
                                } else {
                                   print("\(String(describing: error?.localizedDescription))")
                                }
}

private func showAlertThatLoginAlreadyExists() {
  let alert = UIAlertController(title: "Registration failed!",
                                message: "Login already exists.",
                                preferredStyle: .alert)

  alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))

  present(alert, animated: true, completion: nil)
}

      

User User function. This is the API as a class:

static func getItemByLogin(for userLogin: String,
                          completion: @escaping (_ userItem: UserItem?) -> Void) {
  refToUsersNode.observeSingleEvent(of: .value, with: { snapshot in

     for user in snapshot.children {
        let snapshotValue = (user as! FIRDataSnapshot).value as! [String: AnyObject]
        let login = snapshotValue["login"] as! String // getting login of user

        if login == userLogin {
           let userItem = UserItem(snapshot: user as! FIRDataSnapshot)
           completion(userItem)
           return
        }
     }

     completion(nil) // haven't founded user
  })
}

      

Along the way, you must change login

to username

.

Hope it helps

0


source







All Articles