Skip to content

Commit 8546794

Browse files
CloCkWeRXcesy
andauthoredJan 21, 2024
Recaptcha (#3586)
* add recaptcha on register view Update new.html.haml to add it * Update Gemfile to add recaptcha * Update env-example to show recaptcha * More view corrections for recaptcha * Update registrations_controller.rb to add recaptcha * Update env-example with test config * Recaptcha help text * Fix trailing spaces * Fix trailing space * Add Recaptcha to gemfile.lock * Fixing Gemfile.lock space * Typo on comments in view * Update app/views/devise/registrations/new.html.haml * Fix signup --------- Co-authored-by: Cesy <[email protected]>
1 parent b77df88 commit 8546794

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed
 

‎Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ gem 'faraday_middleware'
128128

129129
gem 'rack-cors'
130130

131+
# for signups as requested by email service
132+
gem 'recaptcha'
133+
131134
# External APIs for data
132135
gem "gbifrb"
133136

‎Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ GEM
477477
rb-fsevent (0.11.2)
478478
rb-inotify (0.10.1)
479479
ffi (~> 1.0)
480+
recaptcha (5.15.0)
480481
redis-client (0.18.0)
481482
connection_pool
482483
regexp_parser (2.9.0)
@@ -718,6 +719,7 @@ DEPENDENCIES
718719
rails-controller-testing
719720
rails_12factor
720721
rake (>= 10.0.0)
722+
recaptcha
721723
responders
722724
rspec-activemodel-mocks
723725
rspec-rails

‎app/controllers/registrations_controller.rb

+21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
class RegistrationsController < Devise::RegistrationsController
44
respond_to :json
55

6+
prepend_before_action :check_captcha, only: [:create] # Change this to be any actions you want to protect with recaptcha.
7+
68
def edit
79
@twitter_auth = current_member.auth('twitter')
810
@flickr_auth = current_member.auth('flickr')
@@ -46,6 +48,25 @@ def destroy
4648
render "edit"
4749
end
4850
end
51+
52+
private
53+
54+
def sign_up_params
55+
params.require(:member).permit(:login_name, :email, :tos_agreement, :newsletter, :password, :password_confirmation)
56+
end
57+
58+
def check_captcha
59+
return if verify_recaptcha # verify_recaptcha(action: 'signup') for v3
60+
61+
self.resource = resource_class.new sign_up_params
62+
resource.validate # Look for any other validation errors besides reCAPTCHA
63+
set_minimum_password_length
64+
65+
respond_with_navigational(resource) do
66+
flash.discard(:recaptcha_error) # We need to discard flash to avoid showing it on the next page reload
67+
render :new
68+
end
69+
end
4970
end
5071

5172
# check if we need the current password to update fields

‎app/views/devise/registrations/new.html.haml

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
%h1 Join #{ENV['GROWSTUFF_SITE_NAME']}
44
.card-body
55
%p Sign up for a #{ENV['GROWSTUFF_SITE_NAME']} account to track your vegetable garden and connect with other local growers.
6+
%p If you have accessibility issues with the captcha, please contact us via the links in the footer and we will help.
67

78
= bootstrap_form_for(resource, as: resource_name, url: registration_path(resource_name),
8-
html: { class: "text-center border border-light p-5" }) do |f|
9+
html: { class: "text-center border border-light p-5", data: { turbo: false } }) do |f|
910
= render 'devise/shared/error_messages', resource: resource
1011

1112
= f.text_field :login_name
@@ -28,4 +29,9 @@
2829

2930
= f.submit "Sign up", class: 'btn btn-block btn-success'
3031

32+
-# START add reCAPTCHA
33+
= flash[:recaptcha_error]
34+
= recaptcha_tags
35+
-# END add reCAPTCHA
36+
3137
.card-footer= render "devise/shared/links"

‎env-example

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# include:
1111
# mapbox_map_id
1212

13-
# To use it, copy application.yml.example to application.yml (which is
13+
# To use it, copy env-example.yml or application.yml.example to application.yml (which is
1414
# .gitignored) and fill in the appropriate values.
1515

1616
# Settings in this file will be available to you as ENV['WHATEVER']
@@ -59,3 +59,14 @@ GROWSTUFF_ELASTICSEARCH="true"
5959
GROWSTUFF_EMAIL='noreply@dev.growstuff.org'
6060
ELASTIC_SEARCH_VERSION="7.5.1-amd64"
6161

62+
# We also now use SMTP2GO in prod and Mailgun in staging
63+
# and recaptcha to solve our email issues after SendGrid stopped working
64+
MAILGUN_SMTP_LOGIN=""
65+
MAILGUN_SMTP_PASSWORD=""
66+
MAILGUN_SMTP_PORT=""
67+
MAILGUN_SMTP_SERVER=""
68+
# These recaptcha values are the official Google test ones from
69+
# https://developers.google.com/recaptcha/docs/faq#id-like-to-run-automated-tests-with-recaptcha.-what-should-i-do
70+
# In production, replace them with real ones
71+
RECAPTCHA_SITE_KEY="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"
72+
RECAPTCHA_SECRET_KEY="6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe"

0 commit comments

Comments
 (0)
Please sign in to comment.