Handling ParseUser.getCurrentUser() when migrating parse

400 views Asked by At

When migrating from Parse there is no longer a concept of a "current user" constrained to each Cloud Code request. In the instructions it is written that if your code uses Parse.User.current(), you should use request.user instead (see instructions below, copy pasted from https://github.com/ParsePlatform/parse-server/wiki/Compatibility-with-Hosted-Parse)

For android I am using ParseUser.getCurrentUser() currently to check for example if the user is logged in if (!ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())). How do I need to rewrite this so that it will work after having migrated from Parse? I am a beginner so I am truly grateful for all the help I can receive.

THE INFORMATION ON THE PARSE SITE: No current user

Each Cloud Code request is now handled by the same instance of Parse Server, therefore there is no longer a concept of a "current user" constrained to each Cloud Code request. If your code uses Parse.User.current(), you should use request.user instead. If your Cloud function relies on queries and other operations being performed within the scope of the user making the Cloud Code request, you will need to pass the user's sessionToken as a parameter to the operation in question.

Consider an messaging app where every Message object is set up with an ACL that only provides read-access to a limited set of users, say the author of the message and the recipient. To get all the messages sent to the current user you may have a Cloud function similar to this one:

// Parse.com Cloud Code

    Parse.Cloud.define('getMessagesForUser', function(request, response) {
      var user = Parse.User.current();

  var query = new Parse.Query('Messages');
  query.equalTo('recipient', user);
  query.find()
    .then(function(messages) {
       response.success(messages);
    });
});

If this function is ported over to Parse Server without any modifications, you will first notice that your function is failing to run because Parse.User.current() is not recognized. If you replace Parse.User.current() with request.user, the function will run successfully but you may still find that it is not returning any messages at all. That is because query.find() is no longer running within the scope of request.user and therefore it will only return publicly-readable objects.

To make queries and writes as a specific user within Cloud Code, you need to pass the user's sessionToken as an option. The session token for the authenticated user making the request is available in request.user.getSessionToken().

The ported Cloud function would now look like this:

// Parse Server Cloud Code
    Parse.Cloud.define('getMessagesForUser', function(request, response) {
      var user = request.user; // request.user replaces Parse.User.current()
      var token = user.getSessionToken(); // get session token from request.user

  var query = new Parse.Query('Messages');
  query.equalTo('recipient', user);
  query.find({ sessionToken: token }) // pass the session token to find()
    .then(function(messages) {
      response.success(messages);
    });
});

br Susanna

0

There are 0 answers