0

I am looking at the Parse documentation, and there is this interesting function I would like to use to make validate for signing up users if the username and email provided are registered (taken) or not. The documentation provides example for the following:

var lotsOfWins = PFQuery(className:"Player")
lotsOfWins.whereKey("wins", greaterThan:150)

var fewWins = PFQuery(className:"Player")
fewWins.whereKey("wins", lessThan:5)

var query = PFQuery.orQueryWithSubqueries([lotsOfWins, fewWins])
query.findObjectsInBackgroundWithBlock {
  (results: [PFObject]?, error: NSError?) -> Void in
  if error == nil {
    // results contains players with lots of wins or only a few wins.
  }
}

my version of it is:

/*Check username and email not taken*/
        let usernameInputCheck = PFQuery(className:"_User")
        usernameInputCheck.whereKey("appUsername", equalTo: self.usernameTxtField.text!)

        let emailInputCheck = PFQuery(className:"_User")
        emailInputCheck.whereKey("email", equalTo:self.emailTxtField.text!)

        let query = PFQuery.orQueryWithSubqueries([usernameInputCheck, emailInputCheck])
        query.findObjectsInBackgroundWithBlock {
            (results: [PFObject]?, error: NSError?) -> Void in
            if error == nil {
                // results contains players with lots of wins or only a few wins.
            }
        }

I want to be able to check if email or username is taken, and if yes set a flag so that I can set message to user saying for example: "Email provided is already registered" or "Username is not available, try a different one".

Thanks for your help in advance.

Update: Here is the link to Parse docs: https://parse.com/docs/ios/guide#queries-compound-queries

2 Answers 2

2

Here is some objective-c that does the trick, which you could easily adapt to Swift.

PFQuery * nameQuery = [PFQuery queryWithClassName:@"user"]
[nameQuery whereKey:"username" equalTo:username];
PFQuery * emailQuery = [PFQuery queryWithClassName:@"user"];
[emailQuery whereKey:"email" equalTo:email];


PFQuery * query = [PFQuery orQueryWithSubqueries:@[nameQuery, emailQuery]

        [query countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
          if(number > 0) {
            //Email address OR username is already in use. 
         }
        }];
Sign up to request clarification or add additional context in comments.

6 Comments

Yes that would check the email is taken or not, but what I was asking is checking if both email and username (or either) are taken. Given that my username column is custom (not default username handled by Parse), this approach would lead to two queries to check for username and email separately.
Check the edit I just did. You can simply add multiple whereKey / equalTo to a query.
Yes indeed you can but then how do you know what message to present to user (username is taken or email is taken)? I already solved it using the above code I originally posted and had a function returning int and based on value, I know it is username or email. Then I set the message and display to user. I really appreciate it that you are trying to help though :)
Thanks again for updating the answer...but still there is no way for knowing which is taken and that's the catch. I will share what I did in case someone else comes along.
Yes, your workaround is the only approach that would work (as far as I know) other than writing some cloud code that pushes that work to the backend. If you use cloud code, you can then make one call from the phone as well.
|
0

Workaround the problem:

let usernameInputCheck = PFQuery(className:"_User") usernameInputCheck.whereKey("appUsername", equalTo: self.usernameTxtField.text!)

    let emailInputCheck = PFQuery(className:"_User")
    emailInputCheck.whereKey("email", equalTo:self.emailTxtField.text!)

    let query = PFQuery.orQueryWithSubqueries([usernameInputCheck, emailInputCheck])
    query.findObjectsInBackgroundWithBlock {
        (results: [PFObject]?, error: NSError?) -> Void in
        if error == nil {
            let checkResult = self.emailOrUsernameIsTaken(results!)
            if(checkResult != 0 && checkResult == 1)
            {
               //username taken
            }else if (checkResult != 0 && checkResult == 2){
               //email taken
            }
        }
    }

and here is the function:

func emailOrUsernameIsTaken(results: [PFObject])->Int
    {
        /*Check if username is taken or if email is taken*/

        var preferenceTaken: Int = 0

        if(results[0]["email"] as! String != "" && results[0]["email"] as! String == self.userObject.email!)
        {
            preferenceTaken = 1
        }else if(results[0]["appUsername"] as! String != "" && results[0]["appUsername"] as! String == self.userObject.username!){
            preferenceTaken = 2
        }

        return preferenceTaken
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.