Skip to content

Commit 9dce2d5

Browse files
committed
added background script support
1 parent efb9e9d commit 9dce2d5

File tree

8 files changed

+136
-60
lines changed

8 files changed

+136
-60
lines changed

README.md

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Please check Google site: https://developers.google.com/recaptcha/docs/faq
7474
- Support custom implementation on request method interface
7575
- Fully supported Vue component
7676
- IP skip list supported
77+
- Support script to be placed in the background of pages for analytics
7778

7879
## Requirement
7980

@@ -92,7 +93,7 @@ This package requires the following dependencies:
9293
Via Composer
9394

9495
``` sh
95-
$ composer require timehunter/laravel-google-recaptcha-v3 "~2.2.0" -vvv
96+
$ composer require timehunter/laravel-google-recaptcha-v3 "~2.2.5" -vvv
9697
```
9798

9899
If your Laravel framework version <= 5.4, please register the service provider under your config file: /config/app.php, otherwise please skip it.
@@ -129,7 +130,7 @@ After installation, you should see a googlerecaptchav3.php in your app/config fo
129130
## Configurations <a name="settings" />
130131
### Setting up your Google reCAPTCHA details in config file
131132

132-
Please register all details in config for site_key and secret_key.
133+
Please register all details in config for host_name, site_key, secret_key and site_verify_url.
133134

134135
Specify your Score threshold and action in 'setting', e.g.
135136
``` php
@@ -138,12 +139,21 @@ Specify your Score threshold and action in 'setting', e.g.
138139
'action' => 'contact_us', // Google reCAPTCHA required paramater
139140
'threshold' => 0.2, // score threshold
140141
'is_enabled' => false // if this is true, the system will do score comparsion against your threshold for the action
141-
]
142+
],
143+
[
144+
'action' => 'signup',
145+
'threshold' => 0.2,
146+
'score_comparision' => true
147+
],
142148
]
143149
```
144150
Note: if you want to enable Score Comparision, you also need to enable is_score_enabled to be true.
145151
``` php
146-
'is_score_enabled' = true
152+
'setting' = [
153+
...
154+
'is_score_enabled' = true
155+
...
156+
]
147157
```
148158

149159
For score comparision, all actions should be registered in googlerecaptchav3 config file under 'setting' section.
@@ -216,66 +226,40 @@ If you manually assign a value to setScore($score), the code will fully skip you
216226

217227

218228
## Blade Usage <a name="blade-basic-usage" />
229+
219230
### Display reCAPTCHA v3
220231

221-
#### Blade
222-
Include div with an ID inside your form, e.g.
232+
#### Blade for Background (optional)
223233

224-
``` html
225-
<div id="contact_us_id"></div>
226-
```
234+
It's recommended to include reCAPTCHA v3 on every page which can help you get the most context about interactions for analytics.
227235

228-
Include Template script in your bottom/header of your page, params should follow 'ID'=>'Action', e.g.
236+
In your main homepage or layout page, put the following script at the bottom of your page:
229237

230-
``` PHP
231-
{!! GoogleReCaptchaV3::render(
232-
[
233-
'contact_us_id'=>'contact_us', // the div id=contact_us_id maps to action name contact_us
234-
'signin_id'=>'registration', // the div id=signin_id maps to action name registration
235-
'register_id'=>'registration' // the div id=register_id maps to action name registration
236-
]) !!}
238+
``` html
239+
{!! GoogleReCaptchaV3::background() !!}
237240
```
238241

239-
#### Example Usage
240-
241-
Register your action in config, also enable score and set up your own site key and secret key:
242+
Note: the above script MUST be called after GoogleReCaptchaV3::render(), otherwise the google script might run multiple times.
242243

244+
You can also set the background reCAPTCHA as hidden or visible(bottomright):
243245

244246
``` php
245-
'setting' => [
246-
[
247-
'action' => 'contact_us',
248-
'threshold' => 2,
249-
'score_comparision' => true
250-
],
251-
[
252-
'action' => 'signup',
253-
'threshold' => 0.2,
254-
'score_comparision' => true
255-
],
256-
]
247+
...
248+
'background_badge_display' => true,
249+
...
257250
```
251+
252+
#### Blade for Form & Action
258253

259-
Register two routes in web.php
260-
``` php
261-
Route::get('/index', 'ReCaptchaController@index');
262-
Route::post('/verify', 'ReCaptchaController@verify');
263-
```
254+
Include div with an ID inside your form, e.g.
264255

265-
Create two functions in controller:
266-
``` php
267-
public function verify(Request $request)
268-
{
269-
dd(GoogleReCaptchaV3::verifyResponse($request->input('g-recaptcha-response'))->getMessage());
270-
}
271-
public function index(Request $request)
272-
{
273-
return view('index');
274-
}
256+
``` html
257+
<div id="contact_us_id"></div>
275258
```
276259

277-
Create your form in index.blade.php:
278-
``` html
260+
Include GoogleReCaptchaV3::render() script after your form, params should follow 'ID'=>'Action', e.g.
261+
262+
``` html
279263
{{--if laravel version <=5.6, please use {{ csrf_field() }}--}}
280264

281265
<form method="POST" action="/verify">
@@ -294,11 +278,8 @@ Create your form in index.blade.php:
294278
{!! GoogleReCaptchaV3::render(['contact_us_id'=>'contact_us', 'signup_id'=>'signup']) !!}
295279
```
296280

297-
The backend request will receive a value for 'g-recaptcha-response'.
298-
299-
Go to /index and click submit button on contact us form and you should see an error message that 'Score does not meet the treshhold' because the threshold >2. You can play around the controller to see all outcomes. Importantly, you need to wait the script to be loaded before clicking the submit button.
300281

301-
## Badge Display <a name="badge-display" />
282+
### Badge Display for Form & Action
302283

303284
If your settings were not reflected, please run php artisan config:cache to clear cache.
304285

@@ -338,7 +319,7 @@ Custom
338319
2. Do Styling/CSS on its div element
339320

340321

341-
## Vue Usage (Pacakge version >= 2.2.0) <a name="vue-usage" />
322+
## Vue Usage (Package version >= 2.2.0) <a name="vue-usage" />
342323

343324
The package provides a lightweight vue component. You need to publish the vue component before playing around it.
344325

config/googlerecaptchav3.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@
6666
*/
6767
'inline' => false,
6868

69+
/*
70+
|--------------------------------------------------------------------------
71+
| Background Badge Style
72+
|--------------------------------------------------------------------------
73+
| Type: boolean
74+
| Support:
75+
| - true: the background badge will be displayed at the bottom right of page
76+
| - false: the background badge will be invisible
77+
|
78+
*/
79+
'background_badge_display' => true,
80+
6981
/*
7082
|--------------------------------------------------------------------------
7183
| Score Comparision
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@if(\TimeHunter\LaravelGoogleReCaptchaV3\GoogleReCaptchaV3::$hasAction === false)
2+
@if($display === false)
3+
<style>
4+
.grecaptcha-badge {
5+
display: none;
6+
}
7+
</style>
8+
@endif
9+
<script>
10+
if (!document.getElementById('gReCaptchaScript')) {
11+
let reCaptchaScript = document.createElement('script');
12+
reCaptchaScript.setAttribute('src', 'https://www.google.com/recaptcha/api.js?render={{$publicKey}}');
13+
reCaptchaScript.async = true;
14+
reCaptchaScript.defer = true;
15+
document.head.appendChild(reCaptchaScript);
16+
}
17+
</script>
18+
@endif

resources/views/googlerecaptchav3/template.blade.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
{{\TimeHunter\LaravelGoogleReCaptchaV3\GoogleReCaptchaV3::setHasAction(true)}}
12
<script>
3+
24
function onloadCallback() {
35
@foreach($mappers as $action=>$fields)
46
@foreach($fields as $field)
57
let client{{$field}} = grecaptcha.render('{{$field}}', {
68
'sitekey': '{{$publicKey}}',
79
@if($inline===true) 'badge': 'inline', @endif
810
'size': 'invisible',
9-
'hl' : '{{$language}}'
11+
'hl': '{{$language}}'
1012
});
1113
grecaptcha.ready(function () {
1214
grecaptcha.execute(client{{$field}}, {
@@ -17,4 +19,5 @@ function onloadCallback() {
1719
@endforeach
1820
}
1921
</script>
20-
<script src="https://www.google.com/recaptcha/api.js?render=explicit&onload=onloadCallback" defer async></script>
22+
<script id='gReCaptchaScript' src="https://www.google.com/recaptcha/api.js?render=explicit&onload=onloadCallback" defer
23+
async></script>

src/Configurations/ReCaptchaConfigV3.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,12 @@ public function getSkipIps()
101101
{
102102
return config('googlerecaptchav3.skip_ips');
103103
}
104+
105+
/**
106+
* @return \Illuminate\Config\Repository|mixed
107+
*/
108+
public function getBackgroundBadgeDisplay()
109+
{
110+
return config('googlerecaptchav3.background_badge_display');
111+
}
104112
}

src/Facades/GoogleReCaptchaV3.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @method static \TimeHunter\LaravelGoogleReCaptchaV3\GoogleReCaptchaV3 setAction($value)
1111
* @method static \TimeHunter\LaravelGoogleReCaptchaV3\GoogleReCaptchaV3 setScore($value)
1212
* @method static render($mappers)
13+
* @method static background()
1314
* @see \TimeHunter\LaravelGoogleReCaptchaV3\GoogleReCaptchaV3
1415
*/
1516
class GoogleReCaptchaV3 extends Facade

src/GoogleReCaptchaV3.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,25 @@ class GoogleReCaptchaV3
1616
{
1717
private $service;
1818
private $defaultTemplate = 'GoogleReCaptchaV3::googlerecaptchav3.template';
19+
private $defaultBackgroundTemplate = 'GoogleReCaptchaV3::googlerecaptchav3.background';
20+
21+
public static $hasAction = false;
1922

2023
public function __construct(GoogleReCaptchaV3Service $service)
2124
{
2225
$this->service = $service;
2326
}
2427

28+
/**
29+
* @param $value
30+
* @return bool
31+
*/
32+
public static function setHasAction($value)
33+
{
34+
self::$hasAction = $value;
35+
return self::$hasAction;
36+
}
37+
2538
/**
2639
* @param $mappers
2740
* @return array
@@ -43,18 +56,53 @@ public function prepareViewData($mappers)
4356
return $data;
4457
}
4558

59+
/**
60+
* @return array
61+
*/
62+
public function prepareBackgroundData()
63+
{
64+
return [
65+
'publicKey' => $this->getConfig()->getSiteKey(),
66+
'display' => $this->getConfig()->getBackgroundBadgeDisplay()
67+
];
68+
}
69+
70+
/**
71+
* @return \Illuminate\Contracts\View\View|mixed
72+
*/
73+
public function background()
74+
{
75+
return app('view')
76+
->make(
77+
$this->getBackgroundView(),
78+
$this->prepareBackgroundData()
79+
);
80+
}
81+
4682
/**
4783
* @param $mappers
4884
* @return \Illuminate\Contracts\View\View|null
4985
*/
50-
public function render($mappers)
86+
public function render($mappers = [])
5187
{
52-
if (! $this->getConfig()->isServiceEnabled()) {
88+
if (!$this->getConfig()->isServiceEnabled()) {
5389
return;
5490
}
55-
$data = $this->prepareViewData($mappers);
5691

57-
return app('view')->make($this->getView(), $data);
92+
return app('view')
93+
->make(
94+
$this->getView(),
95+
$this->prepareViewData($mappers)
96+
);
97+
}
98+
99+
100+
/**
101+
* @return mixed|string
102+
*/
103+
protected function getBackgroundView()
104+
{
105+
return $this->defaultBackgroundTemplate;
58106
}
59107

60108
/**

src/Interfaces/ReCaptchaConfigV3Interface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,9 @@ public function getLanguage();
6969
* @return array
7070
*/
7171
public function getSkipIps();
72+
73+
/**
74+
* @return bool
75+
*/
76+
public function getBackgroundBadgeDisplay();
7277
}

0 commit comments

Comments
 (0)