Fetch user object from session token
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.
+1
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();
});
});
it is not working for me error: httpRequest is not a funcion
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 ;)
Thank You So much it Worked :)
Parse.Cloud.httpRequest is available in parse-server, where did you read it wasn’t?
@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
then why it dosn't worked for me when i tried.... I was using it on nodejs.
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 :)
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.