DarkGhostHunter/Captchavel

[6.x] Remember reCAPTCHA for given minutes

Closed this issue · 1 comments

If a form validation fails, it would be cool to store the reCAPTCHA result for a given number of minutes in the session before asking it again. Adding it as a parameter to the middleware will be breaking change.

We could use it globally in the config file, and disable/enable it programmatically in each middleware.

return [
    'remember' => [
        'key' => null,
        'minutes' => 30,
    ]
];
Route::get('message')->uses(SendMessageController::class)
    ->middleware(ReCaptcha::invisible()->remember(60));

At the middleware, if the remember is active, we will store a key in the session that will expire at a given timestamp. The next request will check if the session contains the key (if this is active) and bypass checking the reCAPTCHA input presence.

For this to work on the frontend, the @unlessrecaptcha method would check if the session contains the reCAPTCHA remember key and is not expired, evaluating the code inside the block if not.

<form>
  @unlessrecaptcha
    <div class="g-recaptcha"
      data-sitekey="_your_site_key_"
      data-callback="onSubmit"
      data-size="invisible">
    </div>
  @endunlessrecaptcha
</form>