Skip to content

Instantly share code, notes, and snippets.

@mikevansnell
Last active October 22, 2018 21:04
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save mikevansnell/5140654 to your computer and use it in GitHub Desktop.
Save mikevansnell/5140654 to your computer and use it in GitHub Desktop.
Parse.com cloudcode sample that enables one user to invite another user, and immediately returns the new user object so that you can do something with it (e.g. start associating objects with it).
// in invite.js module:
exports.inviteUser = function(creatingUser,email,tempPass,response)
{
"use strict";
if (!tempPass) {
tempPass = genRandomPass();
}
var user = new Parse.User();
user.set ("username", email);
user.set ("password", tempPass);
user.set ("email", email);
// TODO: set other user properties. Only the invited user can change them
// after the user has been created.
user.signUp(null, {
success: function(createdUser) {
sendInvitationEmail(email, subject, tempPass, {
success: function(httpResponse) {
console.log("User " + createdUser.id + " created, and sent email: " + httpResponse.status);
response.success(createdUser);
},
error: function (httpResponse) {
console.error("user "+ createdUser.id +" created, but couldn't email them. " + httpResponse.status + " " + httpResponse.text);
response.error("user "+ createdUser.id +" created, but couldn't email them. " + httpResponse.status);
}
});
}
},
error: function(user,error) {
response.error("parse error: couldn't create user " + error.code + " " + error.message);
}
});
};
function sendInvitationEmail(email,subject,tempPass,callbackObject) {
"use strict";
var sendgrid = require("sendgrid");
var secrets = require("cloud/secrets.js");
sendgrid.initialize(secrets.sendgriduser, secrets.sendgridpw); // TODO: your creds here...
var fromname = "My Service";
var from = "noreply@myservice.com";
var subject = "Welcome to My Service";
var template = "hello {email} your temporary password is {pass}" ;
var emailText = template.replace(/{email}/g,email).replace(/{pass}/g,tempPass);
sendgrid.sendEmail({
to: email,
from: from,
fromname: fromname,
subject: subject,
text: emailText,
}, callbackObject);
}
function genRandomPass() {
return "1223"; // TODO: generate a password using a random pw generator
}
// in main.js:
Parse.Cloud.define("inviteUser", function(request, response)
{
var creatingUser = request.user;
var email = request.params.email; // string required
var tempPass = request.params.password; // string
var invitejs = require("cloud/invite.js");
invitejs.inviteUser(creatingUser,email,tempPass,response);
});
@ShaunMerritt
Copy link

@mikevansnell, I really love the idea of this. I am extremely new to cloud code, so I am very confused on how to implement this in objective-c code in my app. Any help would be greatly appreciated. Thanks.

@eric-nodes
Copy link

Hi,
Thanks for your example, this is exactly what i need!

However, after calling inviteUser function in my controller, i cant do anything with the main user :
MainUser -> invite NewUser -> session token is invalid for MainUser (signUp sets the session token to NewUser)

You don't have this problem?
Thanks

@Kinzi
Copy link

Kinzi commented Jan 8, 2016

I have the same issue as @eric-nodes & @Nagesh4you. Did anyone find a solution or workaround?

Other than that: really love this piece of code. Thanks for sharing!

@Kinzi
Copy link

Kinzi commented Jan 8, 2016

Found a solution. Try using:
Parse.Cloud.useMasterKey();

in invite.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment