Skip to content

Instantly share code, notes, and snippets.

@shawkinaw
Last active December 12, 2015 01:59
Show Gist options
  • Save shawkinaw/4695379 to your computer and use it in GitHub Desktop.
Save shawkinaw/4695379 to your computer and use it in GitHub Desktop.
Blog: User Validation on FatFractal
SET ActivateUsersOnReg false
CREATE OBJECTTYPE ActivationRequest (userGuid STRING)
CREATE COLLECTION /ActivationRequest OBJECTTYPE ActivationRequest
PERMIT read:none write:none ON /ActivationRequest
CREATE HANDLER validateUser POST ON /FFUser CREATE AS javascript:require ('scripts/EventHandlers.js').validateUser();
exports.validateUser = function() {
var user = ff.getEventHandlerData();
function ActivationRequest(usr) {
this.clazz = "ActivationRequest";
this.userGuid = usr.guid;
this.createdBy = 'system';
}
var ar = new ActivationRequest(user);
var activateRequest = ff.createObjAtUri(ar, "/ActivationRequest");
var apparentAppAddress = ff.getHttpsAppAddress();
appAddress = apparentAppAddress;
if(appAddress.match(/http.*localhost.*/)) appAddress = ff.getHttpsAppAddress();
var link = appAddress + "/ff/ext/verifyRegistration?guid=" + activateRequest.guid;
var emailMsg = "Welcome! To validate your account, please click on this link: " + link;
ff.sendSMTPEmail("<host>", <port>, <true/false>, <authPort>, "<username>", "<password>", "<fromAddress>", user.email, "<subject>", emailMsg);
}
exports.verifyRegistration = function() {
var data = ff.getExtensionRequestData();
var guid = data.httpParameters['guid'];
if (! guid) {
r.result = null;
r.responseCode = "400";
r.statusMessage = "ActivationRequest guid not supplied";
r.mimeType = "application/json";
return;
}
var r = ff.response();
var activationRequest = ff.getObjFromUri("/ff/resources/ActivationRequest/" + guid);
var user = ff.getUser(activationRequest.userGuid);
if (! user) {
r.result = null;
r.responseCode = "404";
r.statusMessage = "User could not be found";
r.mimeType = "application/json";
return;
}
user.active = true;
ff.updateObj(user);
ff.deleteObj(activationRequest);
r.responseCode = "200";
var hc = require('ringo/httpclient');
var htmlContent = hc.get(ff.getHttpsAppAddress() + '/validateuser.html').content;
htmlContent = htmlContent.replace("___MESSAGE___", "Your message html");
htmlContent = htmlContent.replace("___APP_ADDRESS___", ff.getHttpsAppAddress());
htmlContent = htmlContent.replace("___BASE_URL___", ff.getHttpsAppAddress());
htmlContent = htmlContent.replace("___SUCCESS_ADDRESS___", ff.getHttpsAppAddress() + "/application.html");
htmlContent = '' + htmlContent;
r.result = htmlContent;
r.statusMessage = "User now activated";
r.mimeType = "text/html";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment