Skip to content

Commit 22ca7e0

Browse files
authored
Merge pull request #2 from cedricwalter/master
updated readme
2 parents 0328ff5 + 3c5bded commit 22ca7e0

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

README.md

+51-2
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,58 @@ For having a visible ReCaptcha, you should make two minor changes on the above-m
9898
1. Replace the size prop value `invisible` (see the imported ReCaptcha component) with either `normal` or `compact`. Those will add a checkbox with 'I am not a robot' label.
9999
2. Remove `this.[captchaRef].execute()` lines from your code.
100100

101-
102-
103101
##### Optional props
104102

105103
* `data-theme` - you can add `theme` prop with a value of either `"dark"` or `"light"`(default) to control the background theme of the visible ReCaptcha (when size is `normal` or `compact`)
106104
* `data-badge` - you can send `badge` prop with one of the following values: bottomright (default), bottomleft, inline. This will allowyou to reposition the ReCaptcha badge.
105+
106+
### 3. Save Google response into state or inside a hidden field
107+
108+
```
109+
verifyCallback(recaptchaToken) {
110+
const {
111+
change
112+
} = this.props;
113+
// inside hidden field
114+
change('recaptchaResponse', recaptchaToken);
115+
// or state
116+
this.setState('recaptchaResponse', recaptchaToken)
117+
}
118+
119+
render() {
120+
...
121+
return {
122+
<Field
123+
component={TextField}
124+
name="recaptchaResponse"
125+
type="hidden"
126+
disabled/>
127+
...
128+
}
129+
```
130+
131+
### 4. Implement code server side to validate the response
132+
133+
```
134+
import * as request from 'request'; // from "web-request": "^1.0.7",
135+
136+
async check(recaptchaResponse: string, remoteAddress: string): Promise<boolean> {
137+
const secretKey = "";
138+
return new Promise<boolean>((resolve, reject) => {
139+
const verificationUrl = 'https://www.google.com/recaptcha/api/siteverify?secret=' + secretKey + '&response=' + recaptchaResponse + '&remoteip=' + remoteAddress;
140+
request(verificationUrl
141+
, function(error, response, body) {
142+
if (error) {
143+
return reject(false);
144+
}
145+
if (response.statusCode !== 200) {
146+
return reject(false);
147+
}
148+
149+
body = JSON.parse(body);
150+
const passCaptcha = !(body.success !== undefined && !body.success);
151+
resolve(passCaptcha);
152+
});
153+
});
154+
}
155+
```

0 commit comments

Comments
 (0)