Module: Controllers::TurnstileVerification
- Extended by:
- ActiveSupport::Concern
- Included in:
- ApplicationController
- Defined in:
- app/concerns/controllers/turnstile_verification.rb
Overview
Instance Method Summary collapse
-
#load_turnstile_script_tag ⇒ String
Load Turnstile script tag into the head (only once).
-
#turnstile_lazy_widget(**options) ⇒ String
Lazy-loaded Turnstile widget - only loads script when visible in viewport Use this instead of turnstile_widget + load_turnstile_script_tag for better performance.
-
#turnstile_script_tag ⇒ String
Render the Turnstile script tag.
-
#turnstile_widget(**options) ⇒ String
Render the Turnstile widget.
-
#validate_turnstile! ⇒ Boolean?
Validate Turnstile response and redirect with error if failed Call this from create/update actions that require CAPTCHA validation.
Instance Method Details
#load_turnstile_script_tag ⇒ String
Load Turnstile script tag into the head (only once)
37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/concerns/controllers/turnstile_verification.rb', line 37 def load_turnstile_script_tag return ''.html_safe unless Turnstile.enabled? return ''.html_safe if content_for?(:turnstile_script_loaded) content_for :turnstile_script_loaded, true content_for :head, turnstile_script_tag ''.html_safe rescue NoMethodError # Fallback if content_for is not available (e.g., in tests or console) turnstile_script_tag end |
#turnstile_lazy_widget(**options) ⇒ String
Lazy-loaded Turnstile widget - only loads script when visible in viewport
Use this instead of turnstile_widget + load_turnstile_script_tag for better performance
72 73 74 75 76 |
# File 'app/concerns/controllers/turnstile_verification.rb', line 72 def (**) return ''.html_safe unless Turnstile.enabled? Turnstile.() end |
#turnstile_script_tag ⇒ String
Render the Turnstile script tag
26 27 28 29 30 31 |
# File 'app/concerns/controllers/turnstile_verification.rb', line 26 def turnstile_script_tag return ''.html_safe unless Turnstile.enabled? nonce = request&.content_security_policy_nonce Turnstile.script_tag(nonce: nonce) end |
#turnstile_widget(**options) ⇒ String
Render the Turnstile widget
56 57 58 59 60 |
# File 'app/concerns/controllers/turnstile_verification.rb', line 56 def (**) return ''.html_safe unless Turnstile.enabled? Turnstile.() end |
#validate_turnstile! ⇒ Boolean?
Validate Turnstile response and redirect with error if failed
Call this from create/update actions that require CAPTCHA validation
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/concerns/controllers/turnstile_verification.rb', line 83 def validate_turnstile! # Skip entirely in development/test - production keys don't work on localhost, # and system tests (Playwright) cannot solve a CAPTCHA challenge. return true if Rails.env.local? return true unless Turnstile.enabled? token = params['cf-turnstile-response'] || params['cf_turnstile_response'] ok = Turnstile.verify(token, remote_ip: request.remote_ip) return if ok log_turnstile_failure(token) store_failed_form_data flash[:error] = 'Please complete the security verification to submit the form.' redirect_back_or_to(root_path) end |