3

I've tried updating the same way you would update a PFUser's email and even tried converting obj-c code (from other questions); neither worked. I also have no idea how to use Cloud Code (well...I installed it but I don't know how to pass information into Cloud Code or how to use JavaScript). Is there a way to update a users password without having to send the reset email?

nick9999
  • 601
  • 1
  • 8
  • 22

2 Answers2

8

You can not change a user's password that way for security reasons. You have two choices

  • Password Reset Email

  • Cloud Code Function to Reset the Password

As I understand that you do not know JavaScript, here is a cloud code function that you can use to reset the user's password, as well as a way to call the function using Swift.

Function (in JavaScript):

Parse.Cloud.define("changeUserPassword", function(request, response) {
  // Set up to modify user data
  Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.User);
query.equalTo("username", request.params.username);  // find all the women
query.first({
  success: function(myUser) {
    // Successfully retrieved the object.
myUser.set("password", request.params.newPassword);

myUser.save(null, {
        success: function(myUser) {
          // The user was saved successfully.
          response.success("Successfully updated user.");
        },
        error: function(myUser, error) {
          // The save failed.
          // error is a Parse.Error with an error code and description.
          response.error("Could not save changes to user.");
        }
      });

  },
  error: function(error) {
    alert("Error: " + error.code + " " + error.message);
  }
});
});

Swift code to call the above function:

PFCloud.callFunctionInBackground("changeUserPassword", withParameters: ["username" : "MyCoolUsername", "newPassword" : passwordField.text]) {
  (result: AnyObject?, error: NSError?) -> Void in
  if (error == nil) {
    // result is "Successfully updated user."
  }
}

Good luck!

King-Wizard
  • 15,628
  • 6
  • 82
  • 76
Jacob
  • 2,338
  • 2
  • 22
  • 39
  • Hey thanks! However because I'm using a free account I don't have room for it to be a "job". Is there a way just to make it a function ("define")? – nick9999 Feb 14 '15 at 19:32
  • When I try to change it, it doesn't let me deploy – nick9999 Feb 14 '15 at 19:33
  • Actually, it looks like parse is NOT considering it to be a job. Could you just explain why because I was pretty sure if you put ".job" that makes it a job...not a function.... – nick9999 Feb 14 '15 at 19:36
  • @nick9999 Hi, I apologize I did not mean to put ".job". I meant to make it a function but I was in a hurry when I wrote my answer. – Jacob Feb 14 '15 at 20:16
  • Ok thanks for letting me know...however when I try to deploy, the terminal says there is a syntax error on line 1 – nick9999 Feb 14 '15 at 20:25
  • @nick9999 I have edited the code above. It should say "response" instead of "status" – Jacob Feb 14 '15 at 20:27
  • When I try this code I get the error "Cannot modify user. " I assume is has something to do with the way the MasterKey is used but I'm not sure and haven't been able to figure it out. Anybody else have/solve this issue? – 01Riv Dec 08 '17 at 07:26
1

Yes, password can be changed without Cloud Code and e-mail. After changing "password" field for current user session is reset, but you can restore it by calling PFUser.logInWithUsername again.

let currentUser = PFUser.current()
currentUser!.password = "<new_password>"

currentUser!.saveInBackground() { (successed, error) in
    if successed {
        PFUser.logInWithUsername(inBackground: currentUser!.email!, password: currentUser!.password!) { (user, error) in
            // Your code here...
        }
    }
}
Serhiy
  • 332
  • 6
  • 13