Search code examples
iosobjective-cparse-platformpfuser

iOS Parse validating user


I want to write code in AppDelegate.m that checks if PFUser.currentUser credentials are still valid. The reason I am doing this is for this scenario. Assume user logged in successfully and now currentUser has the basic information of that user. If the user changes the password at some time, when application launch, it should check if those credentials are up to date or not. If email & password doesn't match the one in table, it should log the user out.

I tried to do this but apparently PFUser.currentUser.password is always set to null while PFUser.currentUser.email has the actual value of email used to log in. How can I achieve this validation without the password being accessible?

Here is the code I have for guidance:

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];
        }
    }];

When this failed I tried to NSLog the password and got null so I understood that this was the problem.


Solution

  • I would write a cloud code function using the master key: Parse.Cloud.useMasterKey();

    When you include this in your function you will be able to access/check the user's password and send back a "verified" variable or something of that sort.

    Here is my cloud code function to modify a user, but you can easily modify it to verify a user's info. There are also many answers on the parse forums on this as well as lots of info in the docs.

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