Swift - Parse - Check if username is taken - ios

I am trying to create a function that takes a username as a parameter and checks to see if that username is taken (by comparing it to other PFUsers in the Parse database. This function is in my view controller class. (I know there are similar questions to this but they do not provide quality answers and are more general than this or are not in Swift).
func usernameIsTaken(username: String) -> Bool {
//bool to see if username is taken
var isTaken: Bool = false
//access PFUsers
var query : PFQuery = PFUser.query()!
query.whereKey("User", equalTo: username)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) in
if error == nil {
if (objects!.count > 0){
isTaken = true
println("username is taken")
} else {
println("Username is available. ")
}
} else {
println("error")
}
}
return isTaken
}
The problem is that the condition in the if statement is always false so "Username is available" always prints in the console even if the username is taken."Username is taken" is never printed even when the username is taken. What should I put in the nested if statement to check if the username matches another PFUser?

You are querying for User (class) key, but you need to query for a specific key, for example email.
// First get user's inputted email
let enteredEmailAddress = "sample#gmail.com"
// Then query and compare
var query = PFQuery(className: "_User")
query.whereKey("email", equalTo: enteredEmailAddress)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) in
if error == nil {
if (objects!.count > 0){
isTaken = true
println("username is taken")
} else {
println("Username is available. ")
}
} else {
println("error")
}
}

Just thought I would throw this out there, since this doesn't seem to be well known by people as I've answered a similar question before. Parse does this kind of checking for the user class automatically. If you're trying to create a new user with any of the default fields duplicated in Parse i.e username, email, etc, then Parse will not allow user signup. This is done automatically, with you having to do nothing, except for present the error so the user knows why they weren't able to sign up successfully. An example of signing a user up that checks for username email etc duplicates follows below:
user.signUpInBackgroundWithBlock {
(succeeded: Bool, signupError: NSError?)
-> Void in
if signupError == nil {
//present new controller
println("Signed up")
}
else {
if let errorString = signupError!.userInfo?["error"] as? NSString
{
error = errorString as String
}
else {
error = "We're sorry, an error ocured! Please try again."
}
self.displayAlert("Could not sign up", error: error)
}
}
}

Check the error code. Last time I did this, code 202 = Username Taken, code 203 = e-mail taken.
if signupError == nil {
print("User \(user.username!) signed up OK!")
} else if signupError?.code == 202 {
print("Username taken. Please select another")
} else if signupError?.code == 203 {
print("e-Mail taken. Please select another")
}

Related

Firebase - Unable to execute call to backend

I am working on a function that handles user registration and in the process, check if the selected username entered by the user is taken or not to inform the user to select a different one. I have the below code to accomplish this scenario:
#IBAction func proceedPressed(sender: AnyObject) {
/**********************Perform Validation************************/
if(self.emailTxtField.text != "" && self.passwordTxtField.text != "")
{
print("Email and Password not empty")
self.usernameValidation({(result) -> Void in
if(result == false)
{
print("Result False")
self.usernameErrorLabel.text = "Username Taken"
}else{
print("Result True")
//Username is available...Proceed
self.usernameErrorLabel.text = ""
FIRAuth.auth()?.createUserWithEmail(self.emailTxtField.text!, password: self.passwordTxtField.text!) { (user, error) in
if(error == nil)
{
print("Creating User with Email")
/*Create the user object as submitted*/
self.dbReference.child("users").child(user!.uid).setValue(["username": self.emailTxtField.text!,"name":self.nameTxtField.text!, "email":self.emailTxtField.text!, "mobile":self.mobileTxtField.text!, "homeAddress":"N", "workAddress":"N", "otherAddress":"N", "profilePictureRef":"N","telephone":"0","friendsCount":0, "retailersCount":0])
}else{
print("Error occured: \(error?.description)")
}
}//end of createUserWithEmail
}
})
}else{
print("Error: Email or Password field is empty")
}
}
and to check the username:
func usernameValidation(completion: (result: Bool) -> Void)
{
print("Username is: \(self.usernameTxtField.text!)")
dbReference.child("usernamesTaken").queryOrderedByValue().queryEqualToValue(self.usernameTxtField.text!).observeEventType(.Value, withBlock: { (snapshot: FIRDataSnapshot!) -> Void in
print(snapshot.childrenCount)
if(snapshot.childrenCount == 0)
{
print("result is true in username validation")
//Username Available
completion(result:true)
}else{
print("result is false in username validation")
//Username Taken
completion(result:false)
}
})
}
The problem with the above is that the full code doesn't seem to execute. When button pressed, I get the following messages in console:
- Email and Password not empty
- Username is: [value entered in usernameTxtField.text
and then nothing more. Although I wrote many print statements to try and see where this is stopping, but this is the furthest the code went in terms of printing the statements.
Is there something wrong here that I am missing out?
Thanks in advance.
I did some more testing and then discovered the issue through the xcode console. I copied the following from the firebase website to test fetching the data:
ref.child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user value
let username = snapshot.value!["username"] as! String
let user = User.init(username: username)
// ...
}) { (error) in
print(error.localizedDescription)
}
The above showed an error that is "Permission Denied". Following that I edited the Rules in the database section in the console and allowed .read and .write and that did it. I thought I would post the details just in case someone else gets stuck.

how to properly check for an existing object in parse with swift

I've made a function that creates a "favorite" object back in parse with the tap of a favorite button on the UI :
//MARK: Create the favorite object
func createFavorite(){
let currentUser = PFUser.currentUser()
let currentBook = PFObject(withoutDataWithClassName: "Books", objectId: objectIdSelected)
let favorite = PFObject(className:"Favorites")
favorite["user"] = currentUser
favorite["books"] = currentBook
favorite.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
// success
} else {
// There was a problem, check error.description
}
}
}
Now I am attempting to create a query that checks to see if a favorite object with those exact properties exists using the following logic, I've placed it in the viewDidLoad of a VC that shows a specific book:
//MARK: Check if there is a favorite object available
func isFavoritedByUser(){
let currentUser = PFUser.currentUser()
let currentBook = PFObject(withoutDataWithClassName: "Books", objectId: objectIdSelected)
let query = PFQuery(className:"Favorites")
query.whereKey("user", equalTo: currentUser!)
query.whereKey("books", equalTo: currentBook)
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if objects != nil {
print("this book was favorited by this user")
} else if objects == nil {
print("this book WAS NOT favorited by this user")
} else if error == nil {
// succeed
} else if error != nil {
// There was a problem, check error.description
}
}
}
However, even if no favorite object with those properties exists back in my parse class, it still prints "this book was favorited by this user".. what am i missing here?
The list existing is not proof. That simply proves that some kinda of error weren't observed.
You should check that there was no error first, then you should check that the list of objects has one item in it.
If the list has more than one item then you're creating duplicates and you should fix that...
What you're getting returned is an array, as long as there isn't an error. So, rather than checking for nil, you should be checking the content of the returned array (objects) to see if there is anything in there.
In Parse's guide, you can see what they recommend for this function call:
https://parse.com/docs/ios/guide#queries
Additionally, Parse recommends considering getFirstObjectInBackground instead if this query only ever needs to return a single object, so that might be something to consider as an alternative.
So the problem was i was checking to see if object was nil rather then checking for the count, here is the correct code:
//MARK: Check if there is a favorite object available
func isFavoritedByUser(){
let currentUser = PFUser.currentUser()
let currentBook = PFObject(withoutDataWithClassName: "Books", objectId: objectIdSelected)
let query = PFQuery(className:"Favorites")
query.whereKey("user", equalTo: currentUser!)
query.whereKey("books", equalTo: currentBook)
query.findObjectsInBackgroundWithBlock { (object, error) -> Void in
if error == nil {
if object?.count > 0{
print("this book was favorited by this user")
} else {
print("this book WAS NOT favorited by this user")
}
} else {
}
}
}
For iOS, you can just check the dictionary on an object like so:
rideParse["driver"] == nil
If it exists, then you will get the pointer. If it doesn't/you did not query to include the pointer, then it will just return nil.

How to check UserName taken in parse Xcode

I'm trying to check if username is already taken in parse or not, but seems don't work with my code, can you please what i'm doing wrong on it
Thanks
func usernameIsTaken(userName: String) -> Bool {
let userName = userNameTextField.text
let myUser: PFUser = PFUser.currentUser()!
//bool to see if username is taken
var isTaken : Bool = false
//access PFUsers
let query = PFUser.query()
query!.whereKey("username", equalTo: userName!)
query!.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]? , error : NSError? ) in
if error == nil {
if (objects!.count > 0) {
isTaken = true
print("username is taken")
} else {
print("Username is available. ")
}
} else {
print("error")
}
}
return isTaken
}
For one, you can attempt to sign the user up and Parse will return an error code of 202 if the username has already been taken.
http://parse.com/docs/dotnet/api/html/T_Parse_ParseException_ErrorCode.htm
If this isn't your intended use, to query the User table, use PFUser.query to construct a query instead.
Try this :
query.findObjectsInBackgroundWithBlock({ (object: [PFObject]?,error: NSError?) -> Void in
if error == nil {
}
})
With that being said if your only Interested in the count parse introduced a new method similar to findObjectsInBackground but does exactly what you are looking for, the method is called countObjectsInBackground
You can call this method after you define your query.
like so
query.countObjectsInBackgroundWithBlock { (count: Int32,error: NSError?) -> Void in
if error == nil {
//code here
}
Hope this helps

Letting User Update Password - Parse - Swift

I am trying the code below to let users update their password when a uibutton is clicked. However, ever when I enter the current password right, I get the "wrong current password" message. I guess the system does not compare the entered value with the current password right. Without the first if condition, I can update the password. But I want the user to enter current password for security reasons first. May anyone help?
#IBAction func updatePasswordBtn(sender: AnyObject) {
var passwordCheckQuery = PFQuery(className: "_User")
passwordCheckQuery.whereKey("username", equalTo: PFUser.currentUser()!.username!)
var objects = passwordCheckQuery.findObjects()
for object in objects! {
if currentPassword.text == PFUser.currentUser()!.password {
if newPassword.text == retypeNewPassword.text {
var query6 = PFUser.query()
query6!.whereKey("username", equalTo: PFUser.currentUser()!.username!)
query6!.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
for object6 in objects! {
var ob6:PFObject = object6 as! PFObject
ob6["password"] = self.newPassword.text
ob6.save()
}
}
}
else { println("passwords dont match")
}
}
else { println("wrong current password")
}
}
}
I updated my code as below after #Wains comment and it started to work.
#IBAction func updatePasswordBtn(sender: AnyObject) {
PFUser.logInWithUsernameInBackground(PFUser.currentUser()!.username!, password: currentPassword.text) {
(user:PFUser?, error:NSError?) -> Void in
if error == nil {
var passwordCheckQuery = PFQuery(className: "_User")
passwordCheckQuery.whereKey("username", equalTo: PFUser.currentUser()!.username!)
var objects = passwordCheckQuery.findObjects()
for object in objects! {
if self.newPassword.text == self.retypeNewPassword.text {
var query6 = PFUser.query()
query6!.whereKey("username", equalTo: PFUser.currentUser()!.username!)
query6!.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
for object6 in objects! {
var ob6:PFObject = object6 as! PFObject
ob6["password"] = self.newPassword.text
ob6.save()
println("successfully updated password")
}
}
}
else { println("passwords dont match")
}
}
} else {
println("wrong current password")
}
}
}
The password isn't available to you, so doing PFUser.currentUser()!.password isn't going to give you something you can check against. To verify a password you need to log the user in - i.e. take the user name and password they give you and use PFUser.logInWithUsernameInBackground.... If you get a valid user back then you can set the password and save.

iOS swift parse + how to check if email already exist in database

I'm working on iOS Swift project with Parse. I need to allow users to update their email but it should be unique. Currently, my code looks like following:
var user = PFUser.currentUser()
var userName = user.username
var profQuery = PFQuery(className: "User")
profQuery.whereKey("email", equalTo: fnEditEmail)
profQuery.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil && objects!.count < 1 {
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectId)
object.setValue(self.fnEditEmail.text, forKey: "email")
object.setValue(self.fnEditAge.text, forKey: "age")
object.setValue(self.fnEditGender.text, forKey: "gender")
object.setValue(self.fnEditText.text, forKey: "fullname")
object.setValue(self.keyWord1.text, forKey: "key1")
object.setValue(self.keyWord2.text, forKey: "key2")
object.setValue(self.keyWord3.text, forKey: "key3")
object.setValue(self.keyWord4.text, forKey: "key4")
object.saveInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
if error == nil {
println "Profile Updated."
} else {
println "Failed"
}
}
}
} else if error == nil && objects!.count >= 1 {
println "email already exist."
} else if error != nil {
println "couldn't update, please try again."
}
}
}
I don't think this is correct code and it's not working either. Could somebody please guide me how can I fit this and also, if I can prevent two PFQuery and findObjectsInBackgroundWithBlock, which I think what is required here; One to check if that email exists in current database and one to update the row.
Parse automatically detects this if you try to set the email field of a PFUser. For instance, when signing a user up to your application, Parse will return an error that the email is already being used and won't allow singup. In fact, it even presents the alert for you I'm pretty sure.
In any other part of your app if the user is trying to update their email, Parse will work the same way, albeit without the error presentation which you would have to take care of.
Because of this, you don't have to do any queries or anything. You would simply try to update the email field of a PFUser object, and save it, and Parse will return the error for you if such an email address already exists.
Bottom line is Parse will never allow non-unique email addresses for any PFUser, so you don't have to write code to worry about this. Just worry about email address validation if that's something you want, and then worry about presenting an alert if Parse returns an error.
var user = PFUser.currentUser()
user.setValue(newEmail, forKey: "Email")
user.saveInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
if error == nil {
println "Profile Updated."
} else {
println "Failed"
//present alert to user to let them know that it failed
//ask them to try a new email address
}
}

Resources