From c649d8c997b9e00f5ea1633d2471aebce510c7ca Mon Sep 17 00:00:00 2001 From: Chirag Chhatrala Date: Wed, 18 Dec 2024 18:01:48 +0530 Subject: [PATCH] Refactor registration process to integrate reCAPTCHA - Replaced hCaptcha implementation with reCAPTCHA in RegisterController and related test cases. - Updated validation rules to utilize g-recaptcha-response instead of h-captcha-response. - Modified RegisterForm component to support reCAPTCHA, including changes to the form data structure and component references. - Enhanced test cases to reflect the new reCAPTCHA integration, ensuring proper validation and response handling. These changes improve security and user experience during the registration process by adopting a more widely used captcha solution. --- .../Controllers/Auth/RegisterController.php | 6 ++-- api/tests/Feature/RegisterTest.php | 18 +++++----- .../pages/auth/components/RegisterForm.vue | 34 +++++++------------ 3 files changed, 25 insertions(+), 33 deletions(-) diff --git a/api/app/Http/Controllers/Auth/RegisterController.php b/api/app/Http/Controllers/Auth/RegisterController.php index 447689a1..3870754d 100644 --- a/api/app/Http/Controllers/Auth/RegisterController.php +++ b/api/app/Http/Controllers/Auth/RegisterController.php @@ -12,7 +12,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; use Illuminate\Validation\Rule; -use App\Rules\ValidHCaptcha; +use App\Rules\ValidReCaptcha; class RegisterController extends Controller { @@ -71,8 +71,8 @@ protected function validator(array $data) 'utm_data' => ['nullable', 'array'], ]; - if (config('services.h_captcha.secret_key')) { - $rules['h-captcha-response'] = [new ValidHCaptcha()]; + if (config('services.recaptcha.secret_key')) { + $rules['g-recaptcha-response'] = [new ValidReCaptcha()]; } return Validator::make($data, $rules, [ diff --git a/api/tests/Feature/RegisterTest.php b/api/tests/Feature/RegisterTest.php index 598374cd..ed5aa538 100644 --- a/api/tests/Feature/RegisterTest.php +++ b/api/tests/Feature/RegisterTest.php @@ -1,13 +1,13 @@ Http::response(['success' => true]) + ValidReCaptcha::RECAPTCHA_VERIFY_URL => Http::response(['success' => true]) ]); $this->postJson('/register', [ @@ -17,7 +17,7 @@ 'password' => 'secret', 'password_confirmation' => 'secret', 'agree_terms' => true, - 'h-captcha-response' => 'test-token', // Mock token for testing + 'g-recaptcha-response' => 'test-token', // Mock token for testing ]) ->assertSuccessful() ->assertJsonStructure(['id', 'name', 'email']); @@ -36,7 +36,7 @@ 'email' => 'test@test.app', 'password' => 'secret', 'password_confirmation' => 'secret', - 'h-captcha-response' => 'test-token', + 'g-recaptcha-response' => 'test-token', ]) ->assertStatus(422) ->assertJsonValidationErrors(['email']); @@ -44,7 +44,7 @@ it('cannot register with disposable email', function () { Http::fake([ - ValidHCaptcha::H_CAPTCHA_VERIFY_URL => Http::response(['success' => true]) + ValidReCaptcha::RECAPTCHA_VERIFY_URL => Http::response(['success' => true]) ]); // Select random email @@ -62,7 +62,7 @@ 'password' => 'secret', 'password_confirmation' => 'secret', 'agree_terms' => true, - 'h-captcha-response' => 'test-token', + 'g-recaptcha-response' => 'test-token', ]) ->assertStatus(422) ->assertJsonValidationErrors(['email']) @@ -77,10 +77,10 @@ }); it('requires hcaptcha token in production', function () { - config(['services.h_captcha.secret_key' => 'test-key']); + config(['services.recaptcha.secret_key' => 'test-key']); Http::fake([ - ValidHCaptcha::H_CAPTCHA_VERIFY_URL => Http::response(['success' => true]) + ValidReCaptcha::RECAPTCHA_VERIFY_URL => Http::response(['success' => true]) ]); $this->postJson('/register', [ @@ -92,5 +92,5 @@ 'agree_terms' => true, ]) ->assertStatus(422) - ->assertJsonValidationErrors(['h-captcha-response']); + ->assertJsonValidationErrors(['g-recaptcha-response']); }); diff --git a/client/components/pages/auth/components/RegisterForm.vue b/client/components/pages/auth/components/RegisterForm.vue index 01dd3538..fdf0b8dc 100644 --- a/client/components/pages/auth/components/RegisterForm.vue +++ b/client/components/pages/auth/components/RegisterForm.vue @@ -52,18 +52,16 @@ label="Confirm Password" /> - +
- -
@@ -141,11 +139,10 @@