Letting User Update Password - Parse - Swift - ios

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.

Related

Delete a row from a class in Parse - In Swift

I'm trying to make some voting buttons for my film app.
I use Parse, and I've created a new class called Votes_Up. In this class their are 2 columns: User_Voting and Film_VotedFor.
My code below is actioned when the button is pressed. It takes the currentUser and the film object that the button has been pressed against, and adds them to Parse. My database looks like this:
What I need
so they way it currently works, if the user presses the vote button, it adds the info to the DB and adds 1 to the count. When the user presses it again, it just updates the count by taking 1 away.
I would also want to remove the row it would have added to the database, By the User that voted for it. How can I do this??
My code:
#IBAction func upVoteButton(sender: UIButton) {
let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
let object = objectAtIndexPath(hitIndex)
if PFUser.currentUser() != nil {
if userPressedUpButton == false {
userPressedUpButton = true
let voteUp = PFObject(className: "Votes_Up")
voteUp["User_Voting"] = PFUser.currentUser()
voteUp["Film_VotedFor"] = object!
object!.incrementKey("UpVoteCount")
object!.saveInBackground()
voteUp.saveInBackgroundWithBlock({ (success, error) in
if success == true {
// Object(s) was/were successfully saved
} else if error != nil {
// Display an alert to the user
} else {
// Display an alert to the user - something went wrong
}
})
} else {
userPressedUpButton = false
// I need to removed the row from the class here that was added, as the user has retracted their vote!
object!.incrementKey("UpVoteCount", byAmount: -1)
object!.saveInBackground()
}
} else {
// No user is logged in so they can't vote
performSegueWithIdentifier("Votes_LoginPopup", sender: self)
}
self.tableView.reloadData()
}
Try this:
var query = PFQuery(className:"Votes_Up")
query.whereKey("User_Voting", equalTo:PFUser.currentUser())
query.limit = 1
query.findObjectsInBackgroundWithBlock {
(votesUp: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = votesUp as? [PFObject] {
var firstObject = objects[0]
dispatch_async(dispatch_get_main_queue(),{
// do delete here
firstObject.deleteEventually();
})
}
}
}
or this:
let query = PFQuery(className: "Votes_Up")
query.whereKey("User_Voting", equalTo: PFUser.currentUser())
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
for object in objects {
object.deleteEventually()
}
}

How to create an array of usersnames when working with Parse and Swift

I am trying to create an array of strings for all the usernames using the following code and populate a TableViewController.
class TableViewController: UITableViewController {
var randomUser = [String]()
override func viewDidLoad() {
super.viewDidLoad()
var query: PFQuery = PFUser.query()!
query.findObjectsInBackgroundWithBlock {(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil{
if let objects = (objects as? [PFObject]!){
for object in objects{
self.randomUser.append(object.objectForKey("username") as! String)
print(object.objectForKey("username") as! String)
print(self.randomUser.count)
}
}
}
}
print(self.randomUser.count)
}
the output in the console:
0
username
1
username
2
username
3
But UItableview does not populate.. What could be causing this?
My guess is that query is delayed and view is created before it can return data. Thank you for any help!
Yes, you are right. You need to call self.tableView.reloadData() after you get the results of the query. Below is an example of where to call it.
private var usersArray = [PFUser]()
func fetchUsers() {
let userQuery: PFQuery = PFUser.query()!
userQuery.orderByAscending("username")
userQuery.whereKey("username", notEqualTo: (currentUser?.username)!)
userQuery.findObjectsInBackgroundWithBlock({
(users, error) -> Void in
if error == nil {
self.usersArray = users as! [PFUser]
self.tableView.reloadData()
} else {
print(error)
}
})
}
In this example, you can then access the username property by doing usersArray[i].username

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

Retrieving user data from Parse "_User" class using "ObjectId"

i am trying to retrive a user's data to get the user info from the _User class using the object id. i used this :
var data:NSMutableArray = NSMutableArray()
func loadData() {
data.removeAllObjects()
var profileQuery:PFQuery = PFUser.query()!
profileQuery.getObjectInBackgroundWithId(userId, block: { (objects, error) -> Void in
if error == nil {
self.data.addObject(objects!)
}
})
println(userId) // this is the userId as String.
println(self.data) ********* // empty array.
}
i am getting an empty array data here.. I've tried this also but same thing's happening here too. :
var profileQuery:PFQuery = PFUser.query()!
profileQuery.whereKey("objectId", equalTo: userId)
profileQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
for object in objects {
self.data.addObject(object)
}
}
}
Remember that "findObjectsInBackgroundWithBlock" happens async! You need to put any logic pertaining to the data inside of the block.
var profileQuery:PFQuery = PFUser.query()!
profileQuery.getObjectInBackgroundWithId(userId, block: { (objects, error) -> Void in
if error == nil {
self.data.addObject(objects!)
println(self.data) //shouldn't be empty.
}
})
Putting aside why you're using an NSMutableArray for a singular PFUser object—you're basically expecting to see self.data populated in the wrong location. The user would be added to it (assuming a successful retrieval), inside the closure. So do something like:
var data:NSMutableArray = NSMutableArray()
func loadData() {
data.removeAllObjects()
var profileQuery:PFQuery = PFUser.query()!
profileQuery.getObjectInBackgroundWithId(userId, block: { (objects, error) -> Void in
if error == nil {
self.data.addObject(objects!)
println(self.data) //...shouldn't be empty here
} else {
println("Error retrieving user: \(error.description")
}
})
}

Swift - Parse - Check if username is taken

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")
}

Resources