Use reCAPTCHA v3 for invisible verification
masterreCAPTCHA v3 is an invisible verification method that does not interrupt users. Unlike v2, it requires specifying an action in both the frontend and backend and allows you to define a minimum_score threshold to determine if a user is a human or a robot. If a score falls below your threshold, you can implement fallback logic such as requiring two-factor authentication or showing a v2 checkbox challenge.
To implement a fallback pattern (v3 $\rightarrow$ v2):
- Use
recaptcha_v3in your view. - In your controller, call
verify_recaptchawith aminimum_score. - If v3 fails, attempt
verify_recaptchawithout arguments (v2 mode) and show therecaptcha_tagsin the view if necessary.
# app/controllers/sessions_controller.rb
def create
success = verify_recaptcha(action: 'login', minimum_score: 0.5, secret_key: ENV['RECAPTCHA_SECRET_KEY_V3'])
checkbox_success = verify_recaptcha unless success
if success || checkbox_success
# Perform action
else
if !success
@show_checkbox_recaptcha = true
end
render 'new'
end
end