parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Fetch user object from session token

Open manishsangwan opened this issue 9 years ago • 10 comments

I want to create a middleware that fetches parse user object for given session token. So that i don't need to call cloud function every time.

manishsangwan avatar Jan 19 '17 12:01 manishsangwan

+1

nuno avatar Jan 27 '17 10:01 nuno

Parse.com used to have a request.user already setup for every request... On parse-server, it's not there anymore. I found this code snippet sometime ago that just solves the problem ;) I hope it helps you!

app.use(function (req, res, next) {
  if(!req.session || !req.session.token) {
    return next();
  }
  
  Parse.Cloud.httpRequest({
    url: Parse.serverURL + '/users/me',
    headers: {
      'X-Parse-Application-Id': appid,
      'X-Parse-Session-Token': req.session.token
    }
  }).then(function (userData) {
    req.user = Parse.Object.fromJSON(userData.data);
    next();
  }, function(error) {
    next();
  });
});

lzaiats avatar Feb 08 '17 11:02 lzaiats

it is not working for me error: httpRequest is not a funcion

akshaychauhan7737 avatar Nov 28 '17 09:11 akshaychauhan7737

I think newer versions of Parse.Cloud class does not have httpRequest anymore... You can use some other http lib to make the same request ;)

lzaiats avatar Nov 28 '17 11:11 lzaiats

Thank You So much it Worked :)

akshaychauhan7737 avatar Nov 28 '17 11:11 akshaychauhan7737

Parse.Cloud.httpRequest is available in parse-server, where did you read it wasn’t?

flovilmart avatar Nov 28 '17 12:11 flovilmart

@flovilmart I am very sorry if I spread wrong info, but the new JS API docs does not contains any reference to Parse.Cloud.httpRequest...

The JS SDK API docs used to have this info, and I always used it as my reference.

After doing a more deep search I found that this info (Parse.Cloud.httpRequest) is now on http://docs.parseplatform.org/cloudcode/guide/#networking and I didn't knew that... My fault!

LZ

lzaiats avatar Nov 28 '17 12:11 lzaiats

then why it dosn't worked for me when i tried.... I was using it on nodejs.

akshaychauhan7737 avatar Nov 28 '17 13:11 akshaychauhan7737

I need to add it back but it’s only available when running in CloudCode. Actually, all cloud code methods are missing from the docs now, I need to add them back :)

flovilmart avatar Nov 28 '17 13:11 flovilmart

Does anybody know why I'm getting unauthorised when trying to hit /users/me on my server? I assume I have to have additional headers with the correct keys, but which ones? I can't find any docs on this.

Jauny avatar Oct 16 '18 12:10 Jauny