Last active
October 22, 2018 21:04
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); | |
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
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!
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
@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.