-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Created a config for the credentials * Created a recaptcha-enabled submit button component * Created a validation rule * Added recaptcha to the contact form and login form * Update resources/views/components/recaptcha-submit.blade.php Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Updated recaptcha rule --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
a238196
commit ff2a9c2
Showing
9 changed files
with
129 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Rules; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
|
||
class Recaptcha implements ValidationRule | ||
{ | ||
/** | ||
* Run the validation rule. | ||
* | ||
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail | ||
*/ | ||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
$g_response = \Http::asForm()->post('https://www.google.com/recaptcha/api/siteverify', [ | ||
'secret' => config('recaptcha.api_secret_key'), | ||
'response' => $value, | ||
'remoteip' => request()?->getClientIp(), | ||
]); | ||
if (!$g_response->json('success')) { | ||
$fail("The {$attribute} is invalid."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\View\Components; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\View\View; | ||
use Illuminate\View\Component; | ||
|
||
class RecaptchaSubmit extends Component | ||
{ | ||
/** | ||
* Create a new component instance. | ||
*/ | ||
public function __construct(public string $action, public string $formId) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Get the view / contents that represent the component. | ||
*/ | ||
public function render(): View|Closure|string | ||
{ | ||
return view('components.recaptcha-submit'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* To configure correctly please visit https://developers.google.com/recaptcha/docs/start | ||
*/ | ||
return [ | ||
|
||
/** | ||
* | ||
* The site key | ||
* get site key @ www.google.com/recaptcha/admin | ||
* | ||
*/ | ||
'api_site_key' => env('RECAPTCHA_SITE_KEY', ''), | ||
|
||
/** | ||
* | ||
* The secret key | ||
* get secret key @ www.google.com/recaptcha/admin | ||
* | ||
*/ | ||
'api_secret_key' => env('RECAPTCHA_SECRET_KEY', ''), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<button data-sitekey="{{ config('recaptcha.api_site_key') }}" | ||
data-callback='onSubmit' | ||
data-action='{{ $action }}' | ||
{{ $attributes->merge(['class' => 'g-recaptcha']) }} | ||
> | ||
{{ $slot }} | ||
</button> | ||
@section('javascript') | ||
<script> | ||
let form = document.getElementById("{{ $formId }}"); | ||
function onSubmit(token) { | ||
// Check form validity and submit if valid | ||
if(form.checkValidity()){ | ||
form.submit(); | ||
} else { | ||
// If not valid, create a temporary submit button to trigger form validation | ||
// Create the temporary button, click and remove it | ||
let tmpSubmit = document.createElement('button') | ||
form.appendChild(tmpSubmit) | ||
tmpSubmit.click() | ||
form.removeChild(tmpSubmit) | ||
} | ||
} | ||
</script> | ||
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters