I have written the following query to query a user with a given email and once found, return the session key.
Upon executing it returns an empty response.
I double checked that the user session entry actually exists and is linked to the user I am querying.
var query = new Parse.Query(Parse.User);
var email = request.params.email;
query.equalTo("email",email);
query.first({
success: function(user) {
Parse.Cloud.useMasterKey();
response.success(user.getSessionToken());
},
error: function(user, error) {
response.error(error);
},
useMasterKey: true
});
Ok, since you are using parse.com and not parse-server you need to write
the following line of code:
Parse.Cloud.useMasterKey();
in the first line because you tell cloud code that you want to use master key before you are executing any request to the server. Also it's better to use Promises (according to the best practices)
so at the end your code should look like this:
For running on parse.com
// in parse.com - it's the first thing that you do
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.User);
var email = request.params.email;
query.equalTo("email", email);
query.first().then(function(user) {
response.success(user.getSessionToken());
}, function(error) {
response.error(error);
});
For parse-server
in parse-server you use useMasterKey and send it to the function.
Parse.Cloud.useMasterKey(); will not work here.
// in parse-server - you send useMasterKey to the function
var query = new Parse.Query(Parse.User);
var email = request.params.email;
query.equalTo("email", email);
query.first({
useMasterKey: true
}).then(function(user) {
response.success(user.getSessionToken());
}, function(error) {
response.error(error);
});
Related
I have a function in my cloud code, which works, but I'm not sure how to fix a problem related to it.
Original Problem:
Parse.Cloud.define("assignTokenToUser", function(request, response) {
console.log("Inside assignTokenToUser");
var token = Math.random().toString(30).substring(7);
query = new Parse.Query("User"),
email = request.params.email;
query.equalTo("username", email);
query.find({ useMasterKey: true }).then(function(results) {
query.first({
success: function(user) {
// Successfully retrieved the object.
user.set("emailToken", token);
user.save();
console.log("success...");
response.success(token);
},
error: function(error) {
console.log("error 1...");
response.error(error);
}
});
}, function(error) {
console.log("error 2...");
response.error(error);
});
});
This seemed to be a common problem after scanning the internet, and my analysis is that the useMasterKey needs to be passed each time we use the query object. Correspondingly, my log file shows that when trying to save the user, it gives a Code 206 error.
Log file output:
Inside assignTokenToUser
success...
^[[32minfo^[[39m: Ran cloud function assignTokenToUser for user undefined with:
Input: {"email":"maryam.zafar#emumba.com"}
Result: "p66qm34jd80p0j6ne03fe1q7f" functionName=assignTokenToUser, email=maryam.zafar#emumba.com, user=undefined
going to send an email... with result: p66qm34jd80p0j6ne03fe1q7f
fullLink: https://beatthegym.com/emailVerified?username=maryam.zafar#emumba.com&token=p66qm34jd80p0j6ne03fe1q7f
^[[31merror^[[39m: Error generating response. ParseError { code: 206, message: 'Cannot modify user 4m0VZFsKVt.' } code=206, message=Cannot modify user 4m0VZFsKVt.
[object Object]
So I went on to change my code to the following:
Code:
Parse.Cloud.define("assignTokenToUser", function(request, response) {
console.log("Inside assignTokenToUser");
var token = Math.random().toString(30).substring(7);
query = new Parse.Query("User"),
email = request.params.email;
query.equalTo("username", email);
query.find({ useMasterKey: true }).then(function(results) {
console.log("inside query.find...");
query.first(null, { useMasterKey: true }).then(function(user) {
console.log("inside query.first...");
// Successfully retrieved the object.
user.set("emailToken", token);
user.save(null, { useMasterKey: true }).then(function() {
console.log("inside user.save...");
response.success();
}, function(error) {
response.error(error);
});
response.success(token);
},
function(error) {
console.log("error 1...");
response.error(error);
});
}, function(error) {
console.log("error 2...");
response.error(error);
});
});
Log file:
Inside assignTokenToUser
inside query.find...
inside query.first...
^[[32minfo^[[39m: Ran cloud function assignTokenToUser for user undefined with:
Input: {"email":"maryam.zafar#emumba.com"}
Result: "tqc8m9lo2tcsrqn69c3q0e1q7f" functionName=assignTokenToUser, email=maryam.zafar#emumba.com, user=undefined
inside user.save...
^[[32minfo^[[39m: Ran cloud function assignTokenToUser for user undefined with:
Input: {"email":"maryam.zafar#emumba.com"}
Result: undefined functionName=assignTokenToUser, email=maryam.zafar#emumba.com, user=undefined
[object Object]
Now, the log file gives me a user as "undefined", and the call to the function gives me a pending status in the Chrome Network tab in the Inspector tool, until it turns into 502, and then the request is auto generated by the browser again. All other requests get a correct 200 response.
However, the data seems to be saved.. the record against this email address saves the token generated correctly. But the request from the browser fails and the user is "undefined" while in the original log file, I see the correct user Id... everytime it fails, the function automatically runs again (because the browser is generating another request everytime it gets a 502) and since it is actually supposed to send an email, it's running again and again keeps on generating infinate emails...
Thank you in advance..
Understood this finally:
The user will remain undefined until and unlesss I obtain it using the Parse.User.current() method. The data does save into the database because it is a forced update to the record, however until the user is aunthenticated using the current() method, it will remain undefined.
I see this is an old post but I spotted clear error in the code:
query = new Parse.Query("User")
Should be:
query = new Parse.Query(Parse.User)
Or at least:
query = new Parse.Query("_User")
As User is a predefined class in Parse.
I am migrating an application from parse.com to buddy.com. One of the caveats of the migration was that Parse.User.current() is no longer available on buddy.com, instead you have to get the user and session token from the request itself: https://github.com/ParsePlatform/Parse-Server/wiki/Compatibility-with-Hosted-Parse#no-current-user
The application I am migrating has a logoutUser method that I am attempting to migrate:
Parse.Cloud.define("logoutUser", function(request, response) {
Parse.User.logOut().then(
function onSuccess(result){
response.success(result);
},
function onError(error) {
response.error(error);
}
)
});
now I am attempting to do this in the new style, but am receiving an error. (NOTE: This is cloud code not a nodejs environment)
{
"code":"500",
"error":"Error: There is no current user user on a node.js server environment."
}
New implementation:
function logoutUser(request, response) {
var user = request.user;
var sessionToken = user.getSessionToken();
Parse.User.logOut({ sessionToken }).then(
function onSuccess(result){
response.success(result);
},
function onError(error) {
response.error(error);
}
)
}
Parse.Cloud.define("logoutUser", function(request, response) {
logoutUser(request, response);
});
Suggestions on how to correctly log out users in the Parse on Buddy cloud code?
You could fetch user's session or sessions and delete it / them:
var query = new Parse.Query("_Session");
query.descending('createdAt');
query.equalTo('user', {__type:"Pointer", className:"_User", objectId:"idhere"});
query.first({
useMasterKey: true
}).then(function(session) {
var sessions = [];
sessions.push(session);
Parse.Object.destroyAll(sessions);
}, function (err) {
console.log("Internal error " + err);
});
OR for more tokens you could use find instead of first like:
var query = new Parse.Query("_Session");
query.equalTo('user', {__type:"Pointer", className:"_User", objectId:"idhere"});
query.find({
useMasterKey: true
}).then(function(sessions) {
Parse.Object.destroyAll(sessions);
}, function (err) {
console.log("Internal error " + err);
});
The above will mostly delete or tokens related to the given user. If you wish to delete only tokens used for login, and not for signup or upgrade, then you could put into your query:
query.equalTo('createdWith', { action: 'login', authProvider: 'password'});
As far as i know, deleting a user's last used for login token, then he is logged-out.
To add to the above, if you pass up the user's session key to the Cloud Code function via the X-Parse-Session-Token header, you can use the populated request.user object in the session query directly, instead of the user's ID.
Below is my parse cloud code. I am unable to get user session even when I am using master key. Is it not possible to get User session even if we use mater key to retrieve user?
Parse.Cloud.define("getFSUserSession", getFSUserSession);
function getFSUserSession(request, response) {
Parse.Cloud.useMasterKey();
//Removed some codes here to make it short
var someID = request.params.user;
var query = new Parse.Query(Parse.User);
query.equalTo('familySearchID', someID);
query.ascending('createdAt');
query.first({useMasterKey: true}).then(function (user) {
console.log(typeof user.getSessionToken());//this returns function
console.log(user.getSessionToken()); //this logs No Message provided
response.success(user.getSessionToken());
})
}
I had to disable Require Revocable Sessions to make this work. After disabling Require Revocable Sessions in settings we can make an api request to /1/upgradeToRevocableSession to get the upgraded session token.
Parse.Cloud.define("getFSUserSession", getFSUserSession);
function getFSUserSession(request, response) {
Parse.Cloud.useMasterKey();
//Removed some codes here to make it short
var someID = request.params.user;
var query = new Parse.Query(Parse.User);
query.equalTo('familySearchID', someID);
query.ascending('createdAt');
query.first({useMasterKey: true}).then(function (user) {
return Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://api.parse.com/1/upgradeToRevocableSession',
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-REST-API-Key': restKey,
'X-Parse-Session-Token': user.getSessionToken()
}
});
}).then(function (httpResponse) {
var sessionObject = httpResponse.data;
if (sessionObject.sessionToken) {
return Parse.Promise.as(sessionObject.sessionToken);
}
return Parse.Promise.error('Unable to get Session token');
});
}
I am trying to get the user object from objectId. I know the objectId is valid. But I can get this simple query to work. What is wrong with it? user is still undefined after the query.
var getUserObject = function(userId){
Parse.Cloud.useMasterKey();
var user;
var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo("objectId", userId);
userQuery.first({
success: function(userRetrieved){
console.log('UserRetrieved is :' + userRetrieved.get("firstName"));
user = userRetrieved;
}
});
console.log('\nUser is: '+ user+'\n');
return user;
};
Quick cloud code example using promises. I've got some documentation in there I hope you can follow. If you need more help let me know.
Parse.Cloud.define("getUserId", function(request, response)
{
//Example where an objectId is passed to a cloud function.
var id = request.params.objectId;
//When getUser(id) is called a promise is returned. Notice the .then this means that once the promise is fulfilled it will continue. See getUser() function below.
getUser(id).then
(
//When the promise is fulfilled function(user) fires, and now we have our USER!
function(user)
{
response.success(user);
}
,
function(error)
{
response.error(error);
}
);
});
function getUser(userId)
{
Parse.Cloud.useMasterKey();
var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo("objectId", userId);
//Here you aren't directly returning a user, but you are returning a function that will sometime in the future return a user. This is considered a promise.
return userQuery.first
({
success: function(userRetrieved)
{
//When the success method fires and you return userRetrieved you fulfill the above promise, and the userRetrieved continues up the chain.
return userRetrieved;
},
error: function(error)
{
return error;
}
});
};
The problem with this is that Parse queries are asynchronous. That means that it will return user (null) before the query has time to execute. Whatever you want to do with the user needs to be put inside of the success. Hopefully my explanation helps you understand why it's undefined.
Look into Promises. It's a nicer way of calling something after you get the result from the first query.
I send a request to parse that includes a Comment object that has a pointer to a User named "from".
In afterSave I need to read this and I'm having all kinds of problems. beforeSave works just fine, but I want to execute this code in afterSave;
Parse.Cloud.afterSave("Comment", function(request) {
var userQuery = new Parse.Query("User");
userQuery.get(request.object.get("from").id, {
success: function(user) {
},
error : function(error) {
console.error("errrrrrrrr" + error);
}
});
});
Here is the log I'm seeing on parse
errrrrrrrrr [object Object]
EDIT:
I also tried
var userQuery = new Parse.Query("_User");
Seems like I had to call useMasterKey, since I was fetching a user data.
I'm not entirely sure about this though so I'll keep this question open.
Parse.Cloud.useMasterKey();
Have you tried this?
var userQuery = new Parse.Query(Parse.User);
Try to fetch the pointer directly:
var fromUserPointer = request.object.get("from");
fromUserPointer.fetch().then(function(fetchedFromUser){
},function(error){
});
Slightly different approach.
This assumes that you have the comment object available right there, or at least its id.
Instead of querying the User collection, how about this:
var commentQuery = new Parse.Query("Comment");
commentQuery.include("from");
commentQuery.get(<commentId>, {
success: function (comment)
{
var user = comment.get("from"); // Here you have the user object linked to the comment :)
},
error: function (error)
{
console.log("ERROR: ");
console.log(error);
}
});