Currently signed in user in cloud code

I would like to ask what is the best practise to access currently signed-in user record in cloud code ie. in Parse.Cloud.define function?
Parse.User.current(); returns null and the user attribute on request object is also empty.

Thank you

The concept of a “currently signed in user” is somewhat superfluous in Cloud Code.

You can simply refer to the user object:

const user = await Parse.User.logIn("myname", "mypass");

Thank you.
I’d like to ask what about Parse.Cloud.define(‘definedName’, async(req, res)=>{}); ? How do I get the currently Signed-in Parse.User object?

req.user must return the user who called the function. But if you are using parse server version newer than 3.0 you have to change your cloud code syntax. That’s old syntax

The correct async/await syntax is this, because the response here is what the function returns:

Parse.Cloud.define('definedName', async(req) => {
  return res;

As I said, the concept of a “current user” is not applicable in Cloud Code. It seems what you are trying to do needs refactoring your code. If you have myFunction that requires a user and should be callable as a Cloud Code function and directly from within Cloud Code, you can write it as a separate function.

// This is the function that you want to expose inside and outside of
// Cloud Code.
async function myFunction(user) {
    // ...
    return response;

// This is how you call `myFunction` from outside Cloud Code with the user
// that makes the request.
Parse.Cloud.define('myCloudCodeFunction', async(req) => {
  return await myFunction(req.user);

// This is how you call `myFunction` from inside Cloud Code with the user
// that you logged in.
async function anotherFunction() { 
    const user = await Parse.User.logIn("myname", "mypass");
    return await myFunction(user);

Thank you a lot. Only the thing I struggle with is that I see req.user as undefined even after trying the suggested code. That’s my whole problem. I call the defined function as resolver of graphql query in schema.graphql.

That usually means that

  • you are calling the Cloud Code function from within Cloud Code as there is no “current user” or
  • you are calling the Cloud Code function without a user being logged in

Can you share the code you use to call the Cloud Code function?

If you want to execute some operations as a User from cloud code like saving parse object, excuting a query or many other operation, thanks to the option object you can write something like this.

// In cloud code 
const query = new Parse.Query('SomeClass')'something')
query.find({sessionToken: req.user.getSessionToken()})

Thank you all of your for your help! My issue was that I was sending headers in API Console in GraphQL Console like this:

 "X-Parse-Application-Id": "<app id>",
 "X-Parse-Master-Key": "<maste key>",
 "X-Parse-Session-Token": "<session token>"

And request.user was undefined
after removing the X-Parse-Master-Key header now I can access request.user parse object of user performing the request.

You are totally right, you cannot use a session token and masterkey at the same time :+1:

1 Like