如何在Swift中更改PFUser密码? [英] How to change PFUser password in Swift?
问题描述
我尝试过以与更新PFUser的电子邮件相同的方式进行更新,甚至尝试转换obj-c代码(来自其他问题);都不起作用.我也不知道如何使用Cloud Code(嗯...我安装了它,但是我不知道如何将信息传递到Cloud Code中或如何使用JavaScript).是否可以在无需发送重置电子邮件的情况下更新用户密码?
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?
推荐答案
出于安全原因,您不能以这种方式更改用户密码.您有两种选择
You can not change a user's password that way for security reasons. You have two choices
-
密码重置电子邮件
Password Reset Email
云代码功能以重置密码
据我了解,您不了解JavaScript,这是一个云代码函数,可用于重置用户密码,以及一种使用Swift调用该函数的方法.
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.
函数(在JavaScript中):
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."
}
}
祝你好运!
这篇关于如何在Swift中更改PFUser密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!