-
-
Save pxpgraphics/b813cfd6061d60d7b0ae to your computer and use it in GitHub Desktop.
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); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment