Skip to content

Instantly share code, notes, and snippets.

@mikevansnell
Last active October 22, 2018 21:04
Show Gist options
  • 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 = "[email protected]";
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);
});
@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