-
-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added optional recaptcha v3 support #76
base: master
Are you sure you want to change the base?
Conversation
Hello, @yohanesgultom thanks for contributing. OK, it looks like we need 2 env variables: I have no experience of using Google Recaptcha, could you possibly help to write "How to test instruction" on the description (something like this #50)? Or maybe you could send me a tutorial link for setup google Recaptcha? Thanks. |
Hi @nafiesl . Thanks for your response. This is the link to a complete official guide on Google Recaptcha setup https://developers.google.com/recaptcha/docs/v3. In a nutshell, we just need to:
Let me know if you need more information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @yohanesgultom, I apologize for my very late check on this PR. I left some comments for you. Please have a look when you have time.
Thanks!
/** | ||
* Create a new rule instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this unused method.
/** | |
* Create a new rule instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
// | |
} |
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute | ||
* @param mixed $value | ||
* @return bool | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this docblock.
/** | |
* Determine if the validation rule passes. | |
* | |
* @param string $attribute | |
* @param mixed $value | |
* @return bool | |
*/ |
$response_keys = json_decode($response, true); | ||
if (!$response_keys['success']) { | ||
$this->error_codes = $response_keys['error-codes']; | ||
} | ||
return $response_keys['success']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use camelCase
for variable names.
$response_keys = json_decode($response, true); | |
if (!$response_keys['success']) { | |
$this->error_codes = $response_keys['error-codes']; | |
} | |
return $response_keys['success']; | |
$responseKeys = json_decode($response, true); | |
if (!$responseKeys['success']) { | |
$this->error_codes = $responseKeys['error-codes']; | |
} | |
return $responseKeys['success']; |
/** | ||
* Get the validation error message. | ||
* | ||
* @return string | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this docblock.
/** | |
* Get the validation error message. | |
* | |
* @return string | |
*/ |
public function message() | ||
{ | ||
$msg = __('validation.g_recaptcha_response.failed'); | ||
if (!empty($this->error_codes)) { | ||
$msg = implode(', ', $this->error_codes); | ||
} | ||
return $msg; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not seeing this validation message showing anywhere in the views, maybe you need to add something like these on the views/components/recaptcha-v3.blade.php
:
@if ($errors->has('g-recaptcha-response'))
<div class="form-group has-error">
<div class="col-md-8 col-md-offset-4">
<span class="help-block">
<strong>{{ $errors->first('g-recaptcha-response') }}</strong>
</span>
</div>
</div>
@endif
/** | ||
* Create a new component instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove unused methods.
/** | |
* Create a new component instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
// | |
} |
/** | ||
* Get the view / contents that represent the component. | ||
* | ||
* @return \Illuminate\Contracts\View\View|\Closure|string | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this docblock.
/** | |
* Get the view / contents that represent the component. | |
* | |
* @return \Illuminate\Contracts\View\View|\Closure|string | |
*/ |
'recaptcha' => [ | ||
'site_key' => env('RECAPTCHA_SITE_KEY'), | ||
'secret_key' => env('RECAPTCHA_SECRET_KEY'), | ||
], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this config definition into config/services.php
.
@@ -0,0 +1,15 @@ | |||
@if (!empty(config('app.recaptcha.site_key'))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice, I agree with this.
|
||
use Illuminate\Contracts\Validation\Rule; | ||
|
||
class RecaptchaV3 implements Rule |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not see you adding this validation rule anywhere. I think you need to add this rule to the registration and login controller. Please add this rule to them.
Note:
Make sure you use the !empty(config('app.recaptcha.site_key'))
check. So if our users not filling the site_key, they won't get errors.
Added optional recaptcha support to protect registration and login form from bots