Skip to content

request.user is undefined in hooks when setting useMasterKey: true, even when sessionToken is set #6512

Closed
@jonas-db

Description

@jonas-db

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions