Veerabbhadrayya Math
Veerabbhadrayya Math

Reputation: 45

Parse change user password

I am new to Parse, I am using Parse Js JDK for my webapp. I want to change the password of user. The scenario is: User enter old password, new password & confirm password. On submit of form I want to validate old password if correct proceed to set new password. By using masterKey I trying to reset the user password. Here is the code.

Parse.Cloud.define("changePassword", function(request, response)
{
  Parse.Cloud.useMasterKey();
  var userId = request.params.userId;
  var newPassword = request.params.password;
  var oldPassword = request.params.oldPassword;
  getUser(userId,oldPassword,newPassword,response);
});

function handleError(error,response)
{
  response.error(error);
}

function handleSuccess(notification,response)
{
    var result = {notification:notification};
    response.success(result);
}

function getUser(userId,oldPassword,newPassword,response)
{
  var User = Parse.Object.extend("User");
  var query = new Parse.Query(User);
  query.get(userId, 
  {
    success: function(user) 
    {
        checkOldPassword(user,oldPassword,newPassword,response); 
    },
    error: function(user, error) 
    {
      handleError(error,response);
    }
  });
}

function checkOldPassword(user,oldPassword,newPassword,response)
{
    var username = user.get("username");
    Parse.User.logIn(username, oldPassword,
    {
        success: function(logedIn)
        {
            changePassword(user,newPassword,response);
        },
        error:function(logedIn,error)
        {
            handleError(error,response);
        }
    });
}

function changePassword(user,newPassword,response)
{
    Parse.User.logOut();
    user.set("password",newPassword);
    user.save(null,
    {
        success: function(userObj)
        {
            handleSuccess("User logged out",response);
        },
        error: function(userObj, error) 
        {
            handleError(error,response);
        }
    });
}

Upvotes: 0

Views: 1914

Answers (1)

Orlando gonçalves
Orlando gonçalves

Reputation: 203

Instead of using user.set you must use user.setPassword

http://parseplatform.org/Parse-SDK-JS/api/2.7.1/Parse.User.html#setPassword

Upvotes: 1

Related Questions