Skip to content

Commit 2137c2e

Browse files
authored
Merge pull request #2 from RyanDaDeng/display-style
Display style
2 parents 68689ff + b94f104 commit 2137c2e

File tree

10 files changed

+173
-89
lines changed

10 files changed

+173
-89
lines changed

README.md

Lines changed: 90 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,26 @@
1313

1414
# Importance
1515

16-
If you have any issues or questions on using the package, please use issue tracker to create ticket.
1716

18-
In the future release, I am planning to add the following features:
17+
If you have any issues or questions on using the package, please use issue tracker to create ticket.
1918

20-
- Tests will be covered in future release.
21-
- Request client driver will be configurable.
22-
- Field name will be supported in config file
23-
- Combine reCAPTCHA v3 with v2, if v3 fails then pop-up v2 challenge to do second verification.
24-
- Support VueJS component
2519

2620
## Description
2721

2822
Google reCAPTCHA v3 is a new mechanism to verify whether the user is bot or not.
2923

24+
reCAPTCHA v3 is intended for power users, site owners that want more data about their traffic, and for use cases in which it is not appropriate to show a challenge to the user.
25+
26+
For example, a registration page might still use reCAPTCHA v2 for a higher-friction challenge, whereas more common actions like sign-in, searches, comments, or voting might use reCAPTCHA v3.
27+
28+
Please check Google site: https://developers.google.com/recaptcha/docs/faq
29+
3030
## Features
3131

3232
- Score Comparision
3333
- Support custom implementation on config interface
3434
- Support custom implementation on request method interface
35+
- Support invisible, global and inline badge style
3536

3637
## Requirement
3738

@@ -51,7 +52,7 @@ This package requires the following dependencies:
5152
Via Composer
5253

5354
``` sh
54-
$ composer require timehunter/laravel-google-recaptcha-v3 "^1.1.2"
55+
$ composer require timehunter/laravel-google-recaptcha-v3 "~1.2.0"
5556
```
5657

5758
If your Laravel framework version <= 5.4, please register the service provider in your config file: /config/app.php, otherwise please skip it.
@@ -100,40 +101,90 @@ Note: if you want to enable Score Comparision, you also need to enable is_score_
100101
'is_score_enabled' = true
101102
```
102103

104+
For score comparision, all actions should be registered in googlerecaptchav3 config file under 'setting' section.
105+
103106
For more details please check comments in config file.
104107

105108
#### Display reCAPTCHA v3
106109

107-
Include Google API in header
110+
##### Blade
111+
Include div with an ID inside your form, e.g.
108112

109113
``` html
110-
{!! GoogleReCaptchaV3::requireJs() !!}
114+
<div id="contact_us_id"></div>
111115
```
112116

113-
Include input field
117+
Include Template script in your bottom/header of your page, params should follow 'ID'=>'Action', e.g.
114118

115119
``` PHP
116-
{!! GoogleReCaptchaV3::render($actionName, $fieldName) !!} // $actionName is your google action, $fieldName is optional for input field name
120+
{!! GoogleReCaptchaV3::render(
121+
[
122+
'contact_us_id'=>'contact_us', // the div id=contact_us_id maps to action name contact_us
123+
'signin_id'=>'registration', // the div id=signin_id maps to action name registration
124+
'register_id'=>'registration' // the div id=register_id maps to action name registration
125+
]) !!}
117126
```
118127

119-
Example Usage
128+
##### Example Usage
120129

121130
``` html
122-
{!! GoogleReCaptchaV3::requireJs() !!}
123-
124131
<form method="POST" action="/verify">
125132
@csrf
126-
{!! GoogleReCaptchaV3::render('contact_us') !!}
127-
133+
<div id="contact_us_id"></div>
128134
<input type="submit" value="submit">
135+
<div>
136+
<small>
137+
This site is protected by reCAPTCHA and the Google
138+
<a href="https://policies.google.com/privacy">Privacy Policy</a> and
139+
<a href="https://policies.google.com/terms">Terms of Service</a> apply.
140+
</small>
141+
</div>
129142
</form>
130143

144+
{!! GoogleReCaptchaV3::render([
145+
'contact_us_id'=>'contact_us'
146+
]) !!}
147+
131148
```
132149

133-
Note: For score comparision, all actions should be registered in googlerecaptchav3 config file under 'setting' section.
150+
##### Badge Display
151+
152+
Inline
153+
154+
1. Go to config file, and set
155+
``` PHP
156+
[
157+
...
158+
'inline' => true
159+
...
160+
]
161+
```
162+
2. Badge will be displayed as inline format within the form.
163+
164+
165+
Invisible
166+
167+
1. Set inline as true as well
168+
2. Modify your div with style display:none
169+
3. Refer to Google official site: https://developers.google.com/recaptcha/docs/faq
170+
, you need to include the following text:
171+
``` HTML
172+
This site is protected by reCAPTCHA and the Google
173+
<a href="https://policies.google.com/privacy">Privacy Policy</a> and
174+
<a href="https://policies.google.com/terms">Terms of Service</a> apply.
175+
```
176+
177+
Corner
178+
179+
1. Set inline as false
180+
2. Your badge will be shown in the bottom right side.
181+
182+
Custom
183+
184+
1. Set inline as true
185+
2. Do Styling/CSS on its div element
186+
134187

135-
You can also customise your own template under googlerecaptchav3 folder.
136-
137188
#### Validation Class (Only support Laravel >= 5.5)
138189

139190
You can use provided Validation object to verify your reCAPTCHA.
@@ -179,7 +230,12 @@ Register your action in config, also enable score and set up your own site key a
179230
'action' => 'contact_us',
180231
'threshold' => 2,
181232
'score_comparision' => true
182-
]
233+
],
234+
[
235+
'action' => 'signup',
236+
'threshold' => 0.2,
237+
'score_comparision' => true
238+
],
183239
]
184240
```
185241

@@ -202,18 +258,24 @@ Create two functions in controller:
202258
```
203259

204260
Create your form in index.blade.php:
205-
``` php
206-
{!! GoogleReCaptchaV3::requireJs() !!}
207-
261+
``` html
208262
<form method="POST" action="/verify">
209-
@csrf
210-
{!! GoogleReCaptchaV3::render('contact_us') !!}
263+
@csrf
264+
<div id="contact_us_id"></div>
265+
<input type="submit" value="submit">
266+
</form>
267+
211268

212-
<input type="submit" value="submit">
269+
<form method="POST" action="/verify">
270+
@csrf
271+
<div id="signup_id"></div>
272+
<input type="submit" value="submit">
213273
</form>
274+
275+
{!! GoogleReCaptchaV3::render(['contact_us_id'=>'contact_us', 'signup_id'=>'signup']) !!}
214276
```
215277

216-
Go to /index and click submit button 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 load and render the token before clicking the submit button.
278+
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 load and render the token before clicking the submit button.
217279

218280
## Advanced Usage
219281

config/googlerecaptchav3.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,19 @@
4242
|
4343
*/
4444
'site_key' => '',
45+
4546
/*
4647
|--------------------------------------------------------------------------
47-
| Template
48+
| Badge Style
4849
|--------------------------------------------------------------------------
4950
| Type: string
50-
| Template path, if your template locate at resources/views/template/test.blade.php
51-
| your value should be template.test
51+
| Support:
52+
| - true: the badge will be shown inline with the form, also you can customise your style
53+
| - false: the badge will be shown at the corner of the page
5254
|
5355
*/
54-
'template' => '',
56+
'inline' => true,
57+
5558
/*
5659
|--------------------------------------------------------------------------
5760
| Score Comparision
@@ -95,4 +98,14 @@
9598
| Google reCAPTCHA API
9699
*/
97100
'site_verify_url' => 'https://www.google.com/recaptcha/api/siteverify',
101+
102+
103+
/*
104+
|--------------------------------------------------------------------------
105+
| Site Verify Url
106+
|--------------------------------------------------------------------------
107+
| Type: string
108+
| Google reCAPTCHA API
109+
*/
110+
'display_badge' => true
98111
];

resources/views/googlerecaptchav3/field.blade.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

resources/views/googlerecaptchav3/header.blade.php

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<script>
2+
function onloadCallback() {
3+
@foreach($mappers as $action=>$fields)
4+
@foreach($fields as $field)
5+
let client{{$field}} = grecaptcha.render('{{$field}}', {
6+
'sitekey': '{{$publicKey}}',
7+
@if($inline===true) 'badge': 'inline', @endif
8+
'size': 'invisible'
9+
});
10+
grecaptcha.ready(function () {
11+
grecaptcha.execute(client{{$field}}, {
12+
action: '{{$action}}'
13+
});
14+
});
15+
@endforeach
16+
@endforeach
17+
}
18+
</script>
19+
<script src="https://www.google.com/recaptcha/api.js?render=explicit&onload=onloadCallback" defer async></script>

src/Configurations/ReCaptchaConfigV3.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ public function getSecretKey()
4747
return config('googlerecaptchav3.secret_key');
4848
}
4949

50-
/**
51-
* @return string
52-
*/
53-
public function getTemplate()
54-
{
55-
return config('googlerecaptchav3.template');
56-
}
57-
5850
/**
5951
* @return string
6052
*/
@@ -81,4 +73,12 @@ public function getSetting()
8173
{
8274
return config('googlerecaptchav3.setting');
8375
}
76+
77+
/**
78+
* @return bool
79+
*/
80+
public function isInline()
81+
{
82+
return config('googlerecaptchav3.inline');
83+
}
8484
}

src/GoogleReCaptchaV3.php

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,36 @@ class GoogleReCaptchaV3
1717
private $config;
1818
private $requestClient;
1919
private $action;
20-
private $defaultFieldView = 'GoogleReCaptchaV3::googlerecaptchav3.field';
21-
private $defaultHeaderView = 'GoogleReCaptchaV3::googlerecaptchav3.header';
20+
private $defaultTemplate = 'GoogleReCaptchaV3::googlerecaptchav3.template';
2221
private $request;
2322

2423
public function __construct(ReCaptchaConfigV3Interface $config, RequestClientInterface $requestClient)
2524
{
2625
$this->config = $config;
2726
$this->requestClient = $requestClient;
2827
$this->request = app('request');
28+
2929
}
3030

31+
3132
/**
32-
* @param $action
33-
* @param string $name
34-
* @return mixed|null
33+
* @param $mappers
34+
* @return mixed
3535
*/
36-
public function render($action, $name = 'g-recaptcha-response')
36+
public function render($mappers)
3737
{
38-
if ($this->config->isServiceEnabled() === false) {
39-
return null;
38+
$prepareData = [];
39+
foreach ($mappers as $id => $action) {
40+
$prepareData[$action][] = $id;
4041
}
4142

42-
$data = [
43-
'publicKey' => value($this->config->getSiteKey()),
44-
'action' => $action,
45-
'name' => $name,
46-
'id' => uniqid($name . '-', false)
47-
];
48-
49-
$view = $this->getView();
50-
51-
return app('view')->make($view, $data);
52-
}
53-
54-
public function requireJs()
55-
{
5643
return app('view')->make(
57-
$this->defaultHeaderView,
58-
['publicKey' => $this->getConfig()->getSiteKey()]
44+
$this->getView(),
45+
[
46+
'publicKey' => $this->getConfig()->getSiteKey(),
47+
'mappers' => $prepareData,
48+
'inline' => $this->config->isInline()
49+
]
5950
);
6051
}
6152

@@ -64,12 +55,7 @@ public function requireJs()
6455
*/
6556
protected function getView()
6657
{
67-
$configTemplate = $this->config->getTemplate();
68-
69-
if (!empty($configTemplate)) {
70-
$this->defaultFieldView = $configTemplate;
71-
}
72-
return $this->defaultFieldView;
58+
return $this->defaultTemplate;
7359
}
7460

7561
/**

src/Interfaces/ReCaptchaConfigV3Interface.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ public function isScoreEnabled();
2727
*/
2828
public function getSecretKey();
2929

30-
/**
31-
* @return string
32-
*/
33-
public function getTemplate();
34-
3530
/**
3631
* @return string
3732
*/
@@ -56,4 +51,9 @@ public function getSiteVerifyUrl();
5651
* @return string
5752
*/
5853
public function getHostName();
54+
55+
/**
56+
* @return boolean
57+
*/
58+
public function isInline();
5959
}

0 commit comments

Comments
 (0)