forked from evergreen-library-system/Evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add centralized reCAPTCHA v3 handling and improve OPAC form scripts
- Centralized reCAPTCHA v3 logic into a new `recaptcha.js` module for streamlined verification across forms. - Corrected setting name for enabling reCAPTCHA: `recaptcha.enable` to `recaptcha.enabled`. - Refactored OPAC searchbar template to improve form submission handling and removed inline scripts. - Enhanced `recaptcha.js` with detailed logging for better debugging. - Updated documentation with comprehensive setup SQL and placeholder reCAPTCHA keys for security. Release-Note: Centralize reCAPTCHA v3 handling, improve form scripts, and update documentation. Testing Plan: - Verify reCAPTCHA appears on registration and search forms when enabled. - Confirm form submission is blocked on failed reCAPTCHA. - Check correct reCAPTCHA response logging in the console. Signed-off-by: Ian Skelskey <ianskelskey@gmail.com>
- Loading branch information
1 parent
3da0774
commit b059c7e
Showing
5 changed files
with
151 additions
and
81 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
document.addEventListener('DOMContentLoaded', () => { | ||
function initializeRecaptcha(formId, siteKey, actionName, submitAction) { | ||
console.log('Initializing reCAPTCHA for form:', formId); | ||
const form = document.getElementById(formId); | ||
if (!form) { | ||
console.log('Form not found:', formId); | ||
return; | ||
} | ||
|
||
// Create and append reCAPTCHA container dynamically | ||
const recaptchaContainer = createRecaptchaContainer(); | ||
form.appendChild(recaptchaContainer); | ||
console.log('reCAPTCHA container appended to form:', formId); | ||
|
||
// Add event listener to the form for reCAPTCHA validation | ||
form.addEventListener('submit', event => { | ||
event.preventDefault(); | ||
console.log('Form submit action intercepted:', submitAction); | ||
grecaptcha.ready(() => { | ||
console.log('Executing reCAPTCHA with siteKey:', siteKey, 'and actionName:', actionName); | ||
grecaptcha.execute(siteKey, { action: actionName }) | ||
.then(token => handleRecaptchaToken(token, form)); | ||
}); | ||
}); | ||
|
||
function createRecaptchaContainer() { | ||
const container = document.createElement('div'); | ||
container.id = 'recaptcha-container'; | ||
console.log('Created reCAPTCHA container'); | ||
return container; | ||
} | ||
|
||
function handleRecaptchaToken(token, form) { | ||
console.log('Received reCAPTCHA token:', token); | ||
console.log('Sending reCAPTCHA verification request...'); | ||
const session = new OpenSRF.ClientSession('biblio.recaptcha'); | ||
const request = session.request('biblio.recaptcha.verify', { | ||
token, | ||
org_unit: form.dataset.orgUnit | ||
}); | ||
|
||
request.oncomplete = response => processRecaptchaResponse(response, form); | ||
request.send(); | ||
console.log('reCAPTCHA verification request sent'); | ||
} | ||
|
||
function processRecaptchaResponse(response, form) { | ||
console.log('Processing reCAPTCHA response...'); | ||
let msg; | ||
while ((msg = response.recv())) { | ||
try { | ||
const responseContent = JSON.parse(msg.content()); | ||
console.log('reCAPTCHA response:', responseContent); | ||
if (responseContent.success === 1) { | ||
console.log('reCAPTCHA validation successful'); | ||
form.submit(); | ||
} else { | ||
console.log('reCAPTCHA validation failed'); | ||
alert('reCAPTCHA validation failed. Please try again.'); | ||
} | ||
} catch (error) { | ||
console.error('Error parsing reCAPTCHA response as JSON:', error); | ||
alert('Error in reCAPTCHA validation. Please try again.'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Export the function to be used in other scripts | ||
window.initializeRecaptcha = initializeRecaptcha; | ||
}); |
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