Parse.com procedure for validating password requirements in Parse.Cloud.beforeSave(Parse.User

孤街醉人 提交于 2019-12-12 14:20:17

问题


I am trying to find the best procedure for adding password requirements with Parse.com. It appears the easiest way would be to use a cloud function to execute right before user data is saved. My only caveat is that I want to validate user passwords only when the password is different from what is stored in the db or if the user does not exist in the db.

Parse.Cloud.beforeSave(Parse.User, function(request, response) { ... }

Couple of questions:

  1. Does request.object.existed() work in beforeSave functions as it does with afterSave?
  2. Can I access the user's password (using Parse.Cloud.useMasterKey()) via the request.object in the beforeSave functions? Or do I need to query the user's object inside the function.
  3. In the log the input to the beforeSave function appears to have original and updated keys similar to the json below. However, I was not able to access the original and update json through the request.object. How would I access this data? It would be nice if the only check needed to be performed to verify whether a user's password as changed if a comparison between request.object.original.password !== request.object.updated.password

Sample cloudcode log output:

Input: {"original":{"email":"blah", "firstname" : "blah", "emailVerified":true, "username":"blah", "createdAt":"2014-04-28T23:05:47.452Z", "updatedAt":"2014-0716T01:55:52.907Z",
"objectId":"blah", "sessionToken":"blah"}, "update":{"firstname":"blah2"}}


回答1:


Try something like this:

Parse.Cloud.beforeSave(Parse.User, function(request, response) {

  if (request.object.isNew()) {
      // new object.. 
      response.success();
  } else {
    if (request.object.dirtyKeys().indexOf("password") > -1) {
      // Attempted to change the password.
      response.error('No.');
    }
  }

});


来源:https://stackoverflow.com/questions/24771351/parse-com-procedure-for-validating-password-requirements-in-parse-cloud-beforesa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!