diff --git a/README.md b/README.md index a015320..bb96fb0 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,12 @@ RECAPTCHA_SITE_KEY=site_key RECAPTCHA_SECRET=secret ``` +Run vendor publish to add the captcha.php file to config: + +``` +php artisan vendor:publish --tag=CaptchaConfig +``` + By default, The package will try to load keys from environment. However, you can set them manually: ```php @@ -157,6 +163,130 @@ class CaptchaController extends Controller ## Example +### Laravel User Registration Controller + +**app\Http\Controllers\Auth\RegisterController.php** + +```php +middleware('guest'); + } + + /** + * Get a validator for an incoming registration request. + * + * @param array $data + * @return \Illuminate\Contracts\Validation\Validator + */ + protected function validator(array $data) + { + $messages = [ + 'g-recaptcha-response.required' => 'You must verify that you are not a robot.', + ]; + + return Validator::make($data, [ + 'name' => ['required', 'string', 'max:255'], + 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], + 'password' => ['required', 'string', 'min:8', 'confirmed'], + 'g-recaptcha-response' => ['required', new GoogleRecaptcha] + ], $messages); + } + + /** + * Create a new user instance after a valid registration. + * + * @param array $data + * @return \App\User + */ + protected function create(array $data) + { + return User::create([ + 'name' => $data['name'], + 'email' => $data['email'], + 'password' => Hash::make($data['password']), + ]); + } +} +``` + +**app\Rules\GoogleRecaptcha.php** + +```php +check(request()); + return $response->isVerified(); + } + + /** + * Get the validation error message. + * + * @return string + */ + public function message() + { + return 'Are you a robot?'; + } +} +``` + ## Credits - [Anam Hossain](https://github.com/anam-hossain) diff --git a/src/Captcha.php b/src/Captcha.php index 71a3f46..152b134 100644 --- a/src/Captcha.php +++ b/src/Captcha.php @@ -33,7 +33,11 @@ protected function retrieveSecret(string $secret) { if ($secret) return $secret; - if (env('RECAPTCHA_SECRET')) return env('RECAPTCHA_SECRET'); + if (config('captcha.secret')) { + return config('captcha.secret'); + } elseif (env('RECAPTCHA_SECRET')) { + return env('RECAPTCHA_SECRET'); + } throw new SecretNotFoundException; } diff --git a/src/ServiceProvider/CaptchaServiceProvider.php b/src/ServiceProvider/CaptchaServiceProvider.php index 14b7c6c..fb93e23 100644 --- a/src/ServiceProvider/CaptchaServiceProvider.php +++ b/src/ServiceProvider/CaptchaServiceProvider.php @@ -27,11 +27,15 @@ public function register() */ public function boot() { + $this->publishes([ + __DIR__.'/../config/captcha.php' => config_path('captcha.php'), + ], 'CaptchaConfig'); + Blade::directive('captcha', function ($siteKey = null) { $siteKey = $this->loadSiteKey($siteKey); - return "". + return "". "
"; }); @@ -74,7 +78,9 @@ public function loadSiteKey($siteKey = null) { if ($siteKey) return $siteKey; - if (env('RECAPTCHA_SITE_KEY')) { + if (config('captcha.site_key')) { + return config('captcha.site_key'); + } elseif (env('RECAPTCHA_SITE_KEY')) { return env('RECAPTCHA_SITE_KEY'); } diff --git a/src/config/captcha.php b/src/config/captcha.php new file mode 100644 index 0000000..2065801 --- /dev/null +++ b/src/config/captcha.php @@ -0,0 +1,27 @@ + env('RECAPTCHA_SITE_KEY'), + + /* + |-------------------------------------------------------------------------- + | Secret Key for google's recaptcha + |-------------------------------------------------------------------------- + | + | Here you may provide the secret key for google's recaptcha. + | + */ + + 'secret' => env('RECAPTCHA_SECRET'), + +];