Skip to content

Commit

Permalink
Merge pull request #5 from mjducharme/master
Browse files Browse the repository at this point in the history
fixes for config caching
  • Loading branch information
Anam Hossain authored Aug 24, 2019
2 parents 32a804f + cc5210f commit 73daa13
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 3 deletions.
130 changes: 130 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -157,6 +163,130 @@ class CaptchaController extends Controller

## Example

### Laravel User Registration Controller

**app\Http\Controllers\Auth\RegisterController.php**

```php
<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use App\Rules\GoogleRecaptcha;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/

use RegistersUsers;

/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->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
<?php

namespace App\Rules;

use Anam\Captcha\Captcha;
use Illuminate\Contracts\Validation\Rule;

class GoogleRecaptcha implements Rule
{

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$captcha = new Captcha();
$response = $captcha->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)
Expand Down
6 changes: 5 additions & 1 deletion src/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 8 additions & 2 deletions src/ServiceProvider/CaptchaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<script src='https://www.google.com/recaptcha/api.js'></script>".
return "<script src='https://www.google.com/recaptcha/api.js' async defer></script>".
"<div class='g-recaptcha' data-sitekey='{$siteKey}'></div>";
});

Expand Down Expand Up @@ -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');
}

Expand Down
27 changes: 27 additions & 0 deletions src/config/captcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Site Key for google's recaptcha
|--------------------------------------------------------------------------
|
| Here you may provide the site key for google's recaptcha.
|
*/

'site_key' => 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'),

];

0 comments on commit 73daa13

Please sign in to comment.