Skip to content

Commit 719505a

Browse files
committed
removed unused console.log()
1 parent 68689ff commit 719505a

File tree

8 files changed

+157
-79
lines changed

8 files changed

+157
-79
lines changed

README.md

Lines changed: 86 additions & 23 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.
@@ -104,36 +105,92 @@ For more details please check comments in config file.
104105

105106
#### Display reCAPTCHA v3
106107

107-
Include Google API in header
108+
##### Blade
109+
Include div with an ID inside your form, e.g.
108110

109111
``` html
110-
{!! GoogleReCaptchaV3::requireJs() !!}
112+
<div id="contact_us_2"></div>
111113
```
112114

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

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

119-
Example Usage
126+
##### Example Usage
120127

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

142+
{!! GoogleReCaptchaV3::render(
143+
[
144+
'contact_us_id'=>'contact_us'
145+
]) !!}
146+
131147
```
132148

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

135191
You can also customise your own template under googlerecaptchav3 folder.
136-
192+
193+
137194
#### Validation Class (Only support Laravel >= 5.5)
138195

139196
You can use provided Validation object to verify your reCAPTCHA.
@@ -203,14 +260,20 @@ Create two functions in controller:
203260

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

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

216279
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.

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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<script>
2+
function onloadCallback() {
3+
@foreach($mappers as $action=>$fields)
4+
@foreach($fields as $field)
5+
6+
let client{{$field}} = grecaptcha.render('{{$field}}', {
7+
'sitekey': '{{$publicKey}}',
8+
@if($inline===true) 'badge': 'inline', @endif
9+
'size': 'invisible'
10+
});
11+
12+
grecaptcha.ready(function () {
13+
grecaptcha.execute(client{{$field}}, {
14+
action: '{{$action}}'
15+
});
16+
});
17+
18+
@endforeach
19+
@endforeach
20+
21+
}
22+
</script>
23+
24+
25+
<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)