parse-server: request.user is undefined in hooks when setting useMasterKey: true, even when sessionToken is set

Is your feature request related to a problem? Please describe.

Given the following code:

var object = new Object();
object.set('someField', true);
const result = await object.save(null, { sessionToken: user.get('sessionToken'), useMasterKey : true}).catch(e => e);

and the following beforeSave hook:

Parse.Cloud.beforeSave('Object', async (request) => 
{
	const user = request.user;
	const isMaster = request.master;
	const object = request.object;

	console.log('Ask/BeforeSave')
	debug(user) // undefined
	debug(isMaster) // true

})

Saving the object triggers the beforeSave hook, but the request.user is undefined when using useMasterKey. The problem occurs when Object creation is not allowed by the client, thus I can only create Object with the masterKey. As a result, I don’t have access anymore to request.user in beforeSave and afterSave and thus I don’t know for which user this object was saved. I need this information (ie user id) to execute other side effects.

Describe the solution you’d like Given that both useMasterKey and sessionToken are set, request.user should always include the user on which behalf this request was executed.

Describe alternatives you’ve considered I haven’t found any other solution, besides ticket #6459 .

Additional context using “parse-server”: “^3.9.0”, I will need this feature for an upcoming application, so unless there is a workaround, i’ll be open to write a solution if you can give me some pointers.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

In the trigger you can do something like this:

Parse.Cloud.beforeSaveFile(async (request) => {
  if (request.isMaster) return;
  const file = request.file;


  const fileData = await file.getData();
  //I change file name to user id.
  const newFile = new Parse.File(request.user.id, { base64: fileData });
  return newFile;

});