1

我想在 AppDelegate.m 中编写代码来检查PFUser.currentUser凭据是否仍然有效。我这样做的原因是针对这种情况。假设用户登录成功,现在 currentUser 拥有该用户的基本信息。如果用户在某个时间更改了密码,当应用程序启动时,它应该检查这些凭据是否是最新的。如果电子邮件和密码与表中的不匹配,则应将用户注销。

我试图这样做,但显然PFUser.currentUser.password总是设置为空,而PFUser.currentUser.email电子邮件的实际值用于登录。如何在不访问密码的情况下实现此验证?

这是我的指导代码:

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 所以我明白这是问题所在。

4

2 回答 2

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.
  }
});

});
于 2014-08-14T16:27:34.087 回答
0

如果您想验证您当前的密码,您可以将您的密码存储在本地,然后用于loginWithUsernameInBackground:验证。就像是,

[PFUser logInWithUsernameInBackground:[PFUser currentUser].username
                             password:password
                                block:^(PFUser *user, NSError *error) {
}];
于 2015-06-04T02:35:28.337 回答