refactor: enhance hCaptcha validation and error handling logic#36
Merged
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR enhances the hCaptcha validation flow by introducing conditional logic for both the backend and frontend. The primary goal is to make the security layer more robust while maintaining developer experience, ensuring that the application remains functional and flexible in environments where hCaptcha keys might not be configured (such as local development).
Key Changes
handleHCaptchaValidationto automatically bypass verification ifHCAPTCHA_SECRET_KEYis missing. A warning is logged to the console to ensure visibility without breaking the application flow.h-captcha-responsetoken directly into the validation handler. This centralizes error logging and logic, preventing redundant checks in the main controller.home.ejsto wrap the hCaptcha widget and its external script in a conditional block. The widget now only renders ifHCAPTCHA_SITE_KEYis provided in the metadata.Why this is necessary
A "hard-coded" requirement for hCaptcha can hinder development and deployment in restricted environments. These changes provide:
How to Test
HCAPTCHA_SITE_KEYandHCAPTCHA_SECRET_KEYfrom your.env. Verify that the hCaptcha widget does not appear and that you can still generate documents (check server logs for the "bypass" warning)..env. Verify the widget renders, the script loads, and the backend correctly validates the token.h-captcha-responseand reject the request.