Skip to content

Instantly share code, notes, and snippets.

@pxpgraphics
Forked from mikevansnell/invite.js
Last active August 29, 2015 14:23
Show Gist options
  • Save pxpgraphics/b813cfd6061d60d7b0ae to your computer and use it in GitHub Desktop.
Save pxpgraphics/b813cfd6061d60d7b0ae to your computer and use it in GitHub Desktop.
// 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);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment