karser / KarserRecaptcha3Bundle

Google ReCAPTCHA v3 for Symfony
MIT License
160 stars 22 forks source link

Getting the score? #36

Closed pbowyer closed 3 years ago

pbowyer commented 3 years ago

Thanks for writing this bundle, it's working flawlessly!

I would like to store the score with our form submissions, so I can verify that we've picked a good score_threshold.

I thought that by mapping the form field I would get the details, but it returns a single long string.

->add('captcha', Recaptcha3Type::class, [
                'mapped' => true,
                'constraints' => new Recaptcha3(['message' => 'There were problems with your captcha. Please try again or contact with support and provide following code(s): {{ errorCodes }}']),
                'action_name' => 'manual',
            ])

# Gives
captcha = "03AGdBq27LZb9CZbMtrxPvl4JiscE5B...lots more...9I6j-5XzJS8"

Is it possible to retrieve the CAPTCHA check details?

karser commented 3 years ago

Can you please try this PR? use composer require "karser/karser-recaptcha3-bundle:dev-recaptcha-get-score" to test it.

Inject the Recaptcha3Validator and call getLastResponse()->getScore() after the form was submitted:

<?php

use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3Validator;

class TaskController extends AbstractController
{
    public function new(Request $request, Recaptcha3Validator $recaptcha3Validator): Response
    {
        //...
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            //...
            $score = $recaptcha3Validator->getLastResponse()->getScore();
            //...
        }
        //...
    }
}
karser commented 3 years ago

@pbowyer do you still need this?

pbowyer commented 3 years ago

Hi @karser yes we do, thanks - this works well for us.

karser commented 3 years ago

Great! I just merged and released it.