Compare commits

..

No commits in common. "d162ce8ed6fcccfdc80193ac63af717621ec11ea" and "0825ca85a7934bfe28b6580710acd4ab29d60401" have entirely different histories.

6 changed files with 4 additions and 15 deletions

View File

@ -1,15 +1,9 @@
'use strict'; 'use strict';
var base = module.superModule;
var server = require('server'); var server = require('server');
server.extend(base);
// First, let's extend the ContactUs-Landing route
// to add recaptcha data to the template view
server.append('Landing', function(req, res, next) { server.append('Landing', function(req, res, next) {
res.setViewData({ res.setViewData({
// Recaptcha API script URL
recaptchaUrl: 'https://www.google.com/recaptcha/api.js', recaptchaUrl: 'https://www.google.com/recaptcha/api.js',
siteKey: '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI' // Test Site Key from Google siteKey: '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI' // Test Site Key from Google
}); });
@ -17,26 +11,21 @@ server.append('Landing', function(req, res, next) {
next(); next();
}); });
// Now we need to extend ContactUs-Subscribe, to add our logic server.append('Subscribe', function(req, res, next) {
// that invokes the recaptcha service
// We prepend here to ensure that an invalid response is caught before the email is sent
server.prepend('Subscribe', function(req, res, next) {
var recaptchaService = require('*/cartridge/scripts/services/recaptcha');
// Token automatically added to request by recaptcha // Token automatically added to request by recaptcha
var token = req.form['g-recaptcha-response']; var token = req.form['g-recaptcha-response'];
// Add required parameters for validation call // Add required parameters for validation call
var params = { var params = {
// This is a test secret from Google // This is a test secret from Google
// In practice, do not hardcode this. // In practice, do not hardcode this. It's better to store
// It's better to store in a custom preference or service credential configuration // In a custom preference or service credential Configuration
secret: '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe', secret: '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe', // Test secret key from Google
response: token response: token
}; };
// Calls recaptcha service // Calls recaptcha service
// Returns a Service class instance // Returns a Service class instance
// (start with a hardcoded fail)
var validationResult = recaptchaService.call(params); var validationResult = recaptchaService.call(params);
// Actual response from Google // Actual response from Google