fernet/captcha

This package is abandoned and no longer maintained. No replacement package was suggested.

Google No CAPTCHA reCAPTCHA plugin for Fernet

v0.1 2021-01-31 15:02 UTC

This package is auto-updated.

Last update: 2024-04-06 22:01:41 UTC


README

Google reCAPTCHA plugin for the Fernet framework. The plugin uses the No CAPTCHA reCAPTCHA package.

Set up

Create a Google reCAPTCHA v2 site. Add the configuration to your .env:

CAPTCHA_SITE="paste the site code"
CAPTCHA_SECRET="paste de secret code"

If you set up the plugin manually don't forget to add "fernet/captcha" to the plugins.json file.

Usage

The plugin come with 2 components: and . CaptchaScript is needed to add the Google's script.

<?php
namespace App\Component;

use CaptchaFernet\Captcha;
use Symfony\Component\HttpFoundation\Request;

class TodoForm 
{
    private Captcha $captcha;
    private string $message = 'Press the submit button';

    public function __construct(Captcha $captcha)
    {
        $this->captcha = $captcha;
    }
    
    public function handleSubmit(Request $request)
    {
        if (!$this->captcha->verify($request)) {
            $this->message = 'Captcha ok!';
        } else {
            $this->message = 'Error Captcha';
        }
    }

    public function __toString(): string
    {
        return <<<EOH
            <h1>{$this->message}</h1>
            <form @onSubmit="handleSubmit">
                <Captcha />
                <button>Submit</button>
            </form>
EOH;
    }
}

Don't forget to add the script file.

<?php
namespace App\Component;

class App 
{
    public $preventWrapper = true;

    public function __toString(): string
    {
        return <<<EOH
            <html>
            <body>
                <TodoForm />
                <CaptchaScript />
            </body>
            </html>
EOH;
    }
}