Last active
December 12, 2015 01:59
-
-
Save shawkinaw/4695379 to your computer and use it in GitHub Desktop.
Blog: User Validation on FatFractal
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
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(); |
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
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); | |
} |
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
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