Skip to content

Instantly share code, notes, and snippets.

@gate3
Last active September 14, 2016 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gate3/21f70cff33613c99dad0ce481890bba6 to your computer and use it in GitHub Desktop.
Save gate3/21f70cff33613c99dad0ce481890bba6 to your computer and use it in GitHub Desktop.
Create a parse user from logged in user
Hi,
I recently wanted to create a new user from a logged in user account. That was the easy part, i used cloud code to create the user.
The problem started when i attempted creating another user immediately after or at anytime after creating the current user. I kept
getting an invalid token exception.
After a lot of pondering and fruitless googling. I came to the realization that parse logs in a new user automatically, hence the
error, because the local user and the cloud code user would be different.
So the solution is that, as soon as you create a new user just logout the current user immediately in the success function of your
cloud code. For example:
Parse.Cloud.define('create_user', function(req, res) {
var user = new Parse.User();
for (var k in req.params.data) {
user.set(k, req.params.data[k]);
}
user.signUp(null, {
success: function(user) {
res.success(user)
Parse.User.logOut()//logout current user
},
error: function(user, error) {
// Show the error message somewhere and let the user try again.
res.error("Error: " + error.code + " " + error.message);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment