friendsofhyperf/recaptcha

Google recaptcha for hyperf.

0.1.0 2021-01-08 01:35 UTC

This package is auto-updated.

Last update: 2021-11-25 10:26:31 UTC


README

Latest Stable Version Total Downloads GitHub license

Google recaptcha for hyperf.

Installation

  • Request
composer require friendsofhyperf/recaptcha

Usage

  • Middleware
namespace App\Middleware;

use FriendsOfHyperf\ReCaptcha\Middleware\ReCaptchaMiddleware;

class V3CaptchaMiddleware extends ReCaptchaMiddleware
{
    protected $version = 'v3';
    protected $action = 'register'; 
    protected $score = 0.35; 
    protected $hostname; 
}

class V2CaptchaMiddleware extends ReCaptchaMiddleware
{
    protected $version = 'v2';
    protected $action = 'register'; 
    protected $score = 0.35; 
    protected $hostname; 
}
  • Validator
<?php

namespace App\Controller;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;

class IndexController
{
    /**
     * @Inject()
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;

    public function foo(RequestInterface $request)
    {
        $validator = $this->validationFactory->make(
            $request->all(),
            [
                'g-recaptcha' => 'required|recaptcha:register,0.34,hostname,v3',
            ],
            [
                'g-recaptcha.required' => 'g-recaptcha is required',
                'g-recaptcha.recaptcha' => 'Google ReCaptcha Verify Fails',
            ]
        );

        if ($validator->fails()){
            // Handle exception
            $errorMessage = $validator->errors()->first();  
        }
        // Do something
    }
}