我想在AppDelegate.m中编写代码来检查PFUser.currentUser
凭据是否仍然有效。我这样做的原因是这种情况。假设用户已成功登录,现在currentUser已拥有该用户的基本信息。如果用户在某个时间更改密码,则在应用程序启动时,应检查这些凭据是否是最新的。如果电子邮件&密码与表中的密码不匹配,应该将用户注销。
我尝试这样做,但显然PFUser.currentUser.password
总是设置为null,而PFUser.currentUser.email
具有用于登录的电子邮件的实际值。如果没有密码可以访问,我怎样才能实现此验证?< / p>
以下是我的指导代码:
PFQuery *query = [PFQuery queryWithClassName:@"User"];
[query whereKey:@"objectId" equalTo: PFUser.currentUser.objectId];
[query whereKey:@"password" equalTo: PFUser.currentUser.password];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!object) {
/*Credentials changed! Logout user and request login again*/
NSLog(@"Credentials Changed!");
[PFUser logOut];
[[[navigationController viewControllers] objectAtIndex:0] performSegueWithIdentifier:@"loginSegue" sender:self];
}
else {
/*Credentials are still valid..proceed!*/
NSLog(@"Credentials Correct!");
[[[navigationController viewControllers] objectAtIndex:0] performSegueWithIdentifier:@"skipLoginSegue" sender:self];
}
}];
当这次失败时我尝试了NSLog密码并得到了null,所以我明白这就是问题所在。
答案 0 :(得分:2)
我会使用主密钥编写云代码函数:Parse.Cloud.useMasterKey();
当您在功能中包含此功能时,您将能够访问/检查用户的密码并发回“已验证”变量或类似的内容。
这是我修改用户的云代码功能,但您可以轻松修改它以验证用户的信息。解析论坛上还有很多答案以及文档中的大量信息。
Parse.Cloud.define("editUser", function(request, response) {
//var GameScore = Parse.Object.extend("SchoolHappening");
// Create a new instance of that class.
//var gameScore = new GameScore();
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.User);
query.get(request.params.myUser, {
success: function(myUser) {
// The object was retrieved successfully.
myUser.set("cabinetPosition", request.params.myPosition);
// Save the user.
myUser.save(null, {
success: function(myUser) {
// The user was saved successfully.
response.success("Successfully updated user.");
},
error: function(gameScore, 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(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and description.
}
});
});
答案 1 :(得分:0)
如果要验证当前密码,可以在本地存储密码,然后使用loginWithUsernameInBackground:
进行验证。像,
[PFUser logInWithUsernameInBackground:[PFUser currentUser].username
password:password
block:^(PFUser *user, NSError *error) {
}];