Skip to content

Commit

Permalink
Merge pull request #7 from kaptk2/widget-configurations
Browse files Browse the repository at this point in the history
Closes #1
Closes #2
  • Loading branch information
ryangjchandler authored Dec 13, 2022
2 parents 42f8fe0 + a974b2a commit 4e7c67e
Show file tree
Hide file tree
Showing 19 changed files with 191 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

- name: Dependabot metadata
id: metadata
uses: dependabot/[email protected].5
uses: dependabot/[email protected].4
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

Expand Down
36 changes: 33 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ In your layout file, include the Turnstile scripts using the `@turnstileScripts`
</html>
```

Once that's done, you can use the `@turnstile` directive in `<form>` to output the appropriate markup with your site key configured.
Once that's done, you can use the `<x-turnstile />` component inside of a `<form>` to output the appropriate markup with your site key configured.

```blade
<form action="/" method="POST">
@turnstile()
<x-turnstile />
<button>
Submit
Expand Down Expand Up @@ -101,6 +101,36 @@ public function submit(Request $request)
}
```

### Customizing the widget

You can customize the widget by passing attributes to the `<x-turnstile />` component.

> To learn more about these parameters, refer to [the offical documentation](https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/#configurations).
```blade
<form action="/" method="POST">
<x-turnstile
data-action="login"
data-cdata="sessionid-123456789"
data-callback="callback"
data-expired-callback="expiredCallback"
data-error-callback="errorCallback"
data-theme="dark"
data-tabindex="1"
/>
<button>
Submit
</button>
</form>
```

This package can also integrate seamlessly with [Livewire](https://laravel-livewire.com). Upon successful validation, the property specified inside of `wire:model` will be updated with the Turnstile token.

```blade
<x-turnstile wire:model="yourModel" />
```

## Testing

```bash
Expand All @@ -122,7 +152,7 @@ Please review [our security policy](../../security/policy) on how to report secu
## Credits

- [Ryan Chandler](https://github.com/ryangjchandler)
- [All Contributors](../../contributors)
- [All contributors](../../contributors)

## License

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.5",
"spatie/laravel-ray": "^1.26"
"spatie/laravel-ray": "^1.26",
"spatie/pest-plugin-snapshots": "^1.1"
},
"autoload": {
"psr-4": {
Expand Down
19 changes: 19 additions & 0 deletions resources/views/components/turnstile.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div
class="cf-turnstile"
data-sitekey="{{ config('services.turnstile.key') }}"
@if ($attributes->has('wire:model'))
wire:ignore
data-callback="captchaCallback"
{{ $attributes->filter(fn($value, $key) => ! in_array($key, ['data-callback', 'wire:model'])) }}
@else
{{ $attributes->whereStartsWith('data-') }}
@endif
></div>

@if ($attributes->has('wire:model'))
<script>
function captchaCallback(token) {
@this.set("{{ $attributes->get('wire:model') }}", token);
}
</script>
@endif
7 changes: 3 additions & 4 deletions src/LaravelCloudflareTurnstileServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Blade;
use Illuminate\Validation\Rule;
use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile;
use RyanChandler\LaravelCloudflareTurnstile\View\Components\Turnstile as TurnstileComponent;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand All @@ -26,14 +27,12 @@ public function packageRegistered()

public function packageBooted()
{
Blade::component('turnstile', TurnstileComponent::class);

Blade::directive('turnstileScripts', function () {
return '<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>';
});

Blade::directive('turnstile', function () {
return '<div class="cf-turnstile" data-sitekey="'.config('services.turnstile.key').'"></div>';
});

Rule::macro('turnstile', function () {
return app(Turnstile::class);
});
Expand Down
18 changes: 18 additions & 0 deletions src/View/Components/Turnstile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace RyanChandler\LaravelCloudflareTurnstile\View\Components;

use Illuminate\View\Component;

class Turnstile extends Component
{
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('cloudflare-turnstile::components.turnstile');
}
}
5 changes: 0 additions & 5 deletions tests/ExampleTest.php

This file was deleted.

63 changes: 63 additions & 0 deletions tests/TurnstileBladeDirectiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

use Illuminate\Support\Facades\Blade;
use function Spatie\Snapshots\assertMatchesHtmlSnapshot;

beforeEach(function () {
config()->set('services.turnstile.key', '1x00000000000000000000AA');
config()->set('services.turnstile.secret', '2x0000000000000000000000000000000AA');
});

it('can render a turnstile widget', function () {
$html = Blade::render('<x-turnstile />');

assertMatchesHtmlSnapshot($html);
});

it('can render a turnstile widget with a custom action', function () {
$html = Blade::render('<x-turnstile data-action="test-action" />');

assertMatchesHtmlSnapshot($html);
});

it('can render a turnstile widget with a custom cdata', function () {
$html = Blade::render('<x-turnstile data-cdata="test-cdata" />');

assertMatchesHtmlSnapshot($html);
});

it('can render a turnstile widget with a custom callback', function () {
$html = Blade::render('<x-turnstile data-callback="testCallback" />');

assertMatchesHtmlSnapshot($html);
});

it('can render a turnstile widget with a custom expired callback', function () {
$html = Blade::render('<x-turnstile data-expired-callback="testExpiredCallback" />');

assertMatchesHtmlSnapshot($html);
});

it('can render a turnstile widget with a custom error callback', function () {
$html = Blade::render('<x-turnstile data-error-callback="testErrorCallback" />');

assertMatchesHtmlSnapshot($html);
});

it('can render a turnstile widget with a custom theme', function () {
$html = Blade::render('<x-turnstile data-theme="dark" />');

assertMatchesHtmlSnapshot($html);
});

it('can render a turnstile widget with a tabindex', function () {
$html = Blade::render('<x-turnstile data-tabindex="1" />');

assertMatchesHtmlSnapshot($html);
});

it('can render a turnstile widget with a wire model', function () {
$html = Blade::render('<x-turnstile wire:model="captcha" />');

assertMatchesHtmlSnapshot($html);
});
10 changes: 10 additions & 0 deletions tests/TurnstileScriptBladeDirectiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Illuminate\Support\Facades\Blade;
use function Spatie\Snapshots\assertMatchesHtmlSnapshot;

it('can render a turnstile script snippet', function () {
$html = Blade::render('@turnstileScripts');

assertMatchesHtmlSnapshot($html);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html><body>
<div class="cf-turnstile" data-sitekey="1x00000000000000000000AA"></div>

</body></html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html><body>
<div class="cf-turnstile" data-sitekey="1x00000000000000000000AA" data-action="test-action"></div>

</body></html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html><body>
<div class="cf-turnstile" data-sitekey="1x00000000000000000000AA" data-callback="testCallback"></div>

</body></html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html><body>
<div class="cf-turnstile" data-sitekey="1x00000000000000000000AA" data-cdata="test-cdata"></div>

</body></html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html><body>
<div class="cf-turnstile" data-sitekey="1x00000000000000000000AA" data-error-callback="testErrorCallback"></div>

</body></html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html><body>
<div class="cf-turnstile" data-sitekey="1x00000000000000000000AA" data-expired-callback="testExpiredCallback"></div>

</body></html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html><body>
<div class="cf-turnstile" data-sitekey="1x00000000000000000000AA" data-theme="dark"></div>

</body></html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html><body>
<div class="cf-turnstile" data-sitekey="1x00000000000000000000AA" data-tabindex="1"></div>

</body></html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html><body>
<div class="cf-turnstile" data-sitekey="1x00000000000000000000AA" wire:ignore data-callback="captchaCallback"></div>

<script>
function captchaCallback(token) {
@this.set("captcha", token);
}
</script>
</body></html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><head><script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script></head></html>

0 comments on commit 4e7c67e

Please sign in to comment.