{"id":20428215,"url":"https://github.com/coderflexx/filament-turnstile","last_synced_at":"2025-04-06T20:09:22.373Z","repository":{"id":171322358,"uuid":"647728515","full_name":"coderflexx/filament-turnstile","owner":"coderflexx","description":"Filament Plugin to help implement Cloudflare turnstile into your forms.","archived":false,"fork":false,"pushed_at":"2025-03-01T16:12:53.000Z","size":339,"stargazers_count":23,"open_issues_count":1,"forks_count":13,"subscribers_count":0,"default_branch":"2.x","last_synced_at":"2025-03-30T19:05:20.546Z","etag":null,"topics":["filament","filamentphp","laravel","tall-stack","turnstile","turnstile-authentication"],"latest_commit_sha":null,"homepage":"https://coderflex.com/blog/mastering-filament-turnstile-enhancing-web-security-with-cloudflare-integration","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coderflexx.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-31T11:56:17.000Z","updated_at":"2025-03-28T18:25:42.000Z","dependencies_parsed_at":"2024-04-08T17:30:10.056Z","dependency_job_id":"8af7a7e0-879d-428f-91d0-3c05586156ba","html_url":"https://github.com/coderflexx/filament-turnstile","commit_stats":null,"previous_names":["coderflexx/filament-turnstile"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderflexx%2Ffilament-turnstile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderflexx%2Ffilament-turnstile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderflexx%2Ffilament-turnstile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderflexx%2Ffilament-turnstile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coderflexx","download_url":"https://codeload.github.com/coderflexx/filament-turnstile/tar.gz/refs/heads/2.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247543589,"owners_count":20955865,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["filament","filamentphp","laravel","tall-stack","turnstile","turnstile-authentication"],"created_at":"2024-11-15T07:24:40.173Z","updated_at":"2025-04-06T20:09:22.336Z","avatar_url":"https://github.com/coderflexx.png","language":"PHP","funding_links":[],"categories":["Plugins"],"sub_categories":["Administration \u0026 Management"],"readme":"# Filament Turnstile\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/coderflex/filament-turnstile.svg?style=flat-square)](https://packagist.org/packages/coderflex/filament-turnstile)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/coderflexx/filament-turnstile/run-tests.yml?branch=main\u0026label=tests\u0026style=flat-square)](https://github.com/coderflexx/filament-turnstile/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/coderflexx/filament-turnstile/fix-php-code-style-issues.yml?branch=main\u0026label=code%20style\u0026style=flat-square)](https://github.com/coderflexx/filament-turnstile/actions?query=workflow%3A\"Fix+PHP+code+style+issues\"+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/coderflex/filament-turnstile.svg?style=flat-square)](https://packagist.org/packages/coderflex/filament-turnstile)\n\n\u003c/br\u003e\n\n\u003cimg src=\"https://github.com/coderflexx/filament-turnstile/raw/main/art/thumbnail.png\" alt=\"Login Screen screenshot\" class=\"filament-hidden\"/\u003e\n\n\u003c/br\u003e\n\n**Filament Turnstile** is an essential plugin designed to seamlessly integrate Cloudflare's turnstile into your applications.\n\nThis plugin uses [Laravel Turnstile](https://github.com/coderflexx/laravel-turnstile) under the hood. For detailed information, explore the [Laravel Turnstile README](https://github.com/coderflexx/laravel-turnstile).\n\n## Installation\nInstall the package via Composer:\n\n```bash\ncomposer require coderflex/filament-turnstile\n```\n\nFor users still on **Filament V2**, install the package using:\n\n```bash\ncomposer require coderflex/filament-turnstil \"^1.0\"\n```\n\n## Turnstile Keys\nTo utilize **Cloudflare Turnstile**, obtain your `SiteKey` and `SecretKey` from your Cloudflare Dashboard.\n\nRefer to the [documentation](https://developers.cloudflare.com/turnstile/get-started/#get-a-sitekey-and-secret-key) for detailed instructions.\n\nAfter generating the **keys**, include them in your `.env` file using the following format:\n\n```env\nTURNSTILE_SITE_KEY=1x00000000000000000000AA\nTURNSTILE_SECRET_KEY=1x0000000000000000000000000000000AA\n```\n\nFor testing purposes, you can use [Dummy site keys and secret keys](https://developers.cloudflare.com/turnstile/reference/testing/) provided by Cloudflare.\n\n## Usage\n\nUtilizing this plugin is incredibly straightforward. In your form, incorporate the following code:\n\n```php\nuse Coderflex\\FilamentTurnstile\\Forms\\Components\\Turnstile;\n\nTurnstile::make('captcha')\n    -\u003etheme('auto') // accepts light, dark, auto\n    -\u003elanguage('en-US') // see below\n    -\u003esize('normal'), // accepts normal, compact\n```\n\nFor a list of supported languages, refer to the [supported languages section](https://developers.cloudflare.com/turnstile/reference/supported-languages/). \n\nThe `Turnstile` field offers various options; you can learn more about them in [the Cloudflare configuration section](https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/#configurations).\n\n## Turnstile Events\n\nThe Turnstile package provides events that you can leverage to manage the behavior of the captcha in various scenarios.\n\n**Reset Event**\n\nThe `reset-captcha` event allows you to programmatically reset the captcha challenge. This can be useful when you want to:\n\n- **Clear the challenge after a successful form submission:** This ensures a fresh captcha for the next user.\n- **Reset the challenge upon validation errors:** Prevents users from being stuck with a previously solved captcha after encountering errors during form submission.\n\n**Dispatching the Reset Event:**\n\nThere are two primary ways to dispatch the `reset-captcha` event:\n\n**1. Using `onValidationError` Method:**\n\nFilament provides the `onValidationError` method within your form's Livewire component. This method is automatically triggered whenever form [validation fails](https://filamentphp.com/docs/3.x/forms/validation#sending-validation-notifications). Here's how to utilize it:\n\n```php\nprotected function onValidationError(ValidationException $exception): void\n{\n    $this-\u003edispatch('reset-captcha');\n\n    // Perform additional actions as necessary (e.g., display error messages)\n}\n```\n\nIn this example, the `reset-captcha` event is dispatched upon validation errors, ensuring the captcha is reset for the user's next attempt.\n\n**2. Manual Dispatching:**\n\nFor scenarios where resetting the captcha is not directly tied to validation, you can manually dispatch the event using Filament's event dispatcher:\n\n```php\n$this-\u003edispatch('reset-captcha');\n```\n\n**Using Reset Event in Login Page:**\n\nTo automatically reset the captcha on a failed login attempt in your login form's Livewire component, leverage the `throwFailureValidationException` method:\n\n```php\nprotected function authenticate(): void\n{\n    // Perform authentication logic\n    // ...\n\n    if (! Auth::attempt($this-\u003edata)) {\n        $this-\u003ethrowFailureValidationException(\n            [\n                'email' =\u003e 'Invalid email or password.',\n            ]\n        );\n    }\n\n    // Redirect to success page or perform other actions\n}\n```\n\nBy throwing a validation exception with appropriate error messages, you trigger the `onValidationError` method, which in turn dispatches the `reset-captcha` event, effectively resetting the captcha for the next login attempt.\n\n## Real-Life Example:\n\nTo implement the **Turnstile** captcha with the `Login` page in Filament, follow these steps:\n\nCreate a new `App/Filament/Pages/Auth/Login.php` class:\n\n```php\n\nnamespace App\\Filament\\Pages\\Auth;\n\nuse Coderflex\\FilamentTurnstile\\Forms\\Components\\Turnstile;\nuse Filament\\Forms\\Form;\nuse Filament\\Http\\Responses\\Auth\\Contracts\\LoginResponse;\nuse Filament\\Pages\\Auth\\Login as AuthLogin;\n\nclass Login extends AuthLogin\n{\n    /**\n     * @return array\u003cint|string, string|Form\u003e\n     */\n    protected function getForms(): array\n    {\n        return [\n            'form' =\u003e $this-\u003eform(\n                $this-\u003emakeForm()\n                    -\u003eschema([\n                        $this-\u003egetEmailFormComponent(),\n                        $this-\u003egetPasswordFormComponent(),\n                        $this-\u003egetRememberFormComponent(),\n                        Turnstile::make('captcha')\n                            -\u003elabel('Captcha')\n                            -\u003etheme('auto'),\n                    ])\n                    -\u003estatePath('data'),\n            ),\n        ];\n    }\n\n    // if you want to reset the captcha in case of validation error\n    protected function throwFailureValidationException(): never\n    {\n        $this-\u003edispatch('reset-captcha');\n\n        parent::throwFailureValidationException();\n    }\n}\n```\n\nThen, override the `login()` method in your `PanelProvider` (e.g., `AdminPanelProvider`):\n\n```php\nnamespace App\\Providers\\Filament;\n\nuse App\\Filament\\Auth\\Login;\nuse Filament\\Panel;\nuse Filament\\PanelProvider;\n\nclass AdminPanelProvider extends PanelProvider\n{\n    public function panel(Panel $panel): Panel\n    {\n        return $panel\n            -\u003edefault()\n            -\u003eid('admin')\n            -\u003epath('admin')\n            -\u003elogin(Login::class); // override the login page class.\n            ...\n    }\n}\n```\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Oussama](https://github.com/ousid)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [the License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoderflexx%2Ffilament-turnstile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoderflexx%2Ffilament-turnstile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoderflexx%2Ffilament-turnstile/lists"}