@hcaptcha/react-hcaptcha 1.0.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/dist/index.js +106 -34
- package/package.json +1 -1
- package/src/index.js +85 -12
- package/types/index.d.ts +8 -2
package/README.md
CHANGED
|
@@ -140,13 +140,19 @@ return <HCaptcha ref={captchaRef} onLoad={onLoad} sitekey={sitekey} {...props} /
|
|
|
140
140
|
|`onVerify`|`token, eKey`|When challenge is completed. The response `token` and an `eKey` (session id) are passed along.|
|
|
141
141
|
|`onExpire`|-|When the current token expires.|
|
|
142
142
|
|`onLoad`|-|When the hCaptcha API loads.|
|
|
143
|
+
|`onOpen`|-|When the user display of a challenge starts.|
|
|
144
|
+
|`onClose`|-|When the user dismisses a challenge.|
|
|
145
|
+
|`onChalExpired`|-|When the user display of a challenge times out with no answer.|
|
|
143
146
|
|
|
144
147
|
### Methods
|
|
145
148
|
|
|
146
149
|
|Method|Description|
|
|
147
150
|
|---|---|
|
|
148
151
|
|`execute()`|Programmatically trigger a challenge request. Additionally, this method can be run asynchronously and returns a promise with the `token` and `eKey` when the challenge is completed.|
|
|
152
|
+
|`getRespKey()`|Get the current challenge reference ID|
|
|
153
|
+
|`getResponse()`|Get the current challenge response token from completed challenge|
|
|
149
154
|
|`resetCaptcha()`|Reset the current challenge|
|
|
155
|
+
|`setData()`|See enterprise docs.|
|
|
150
156
|
|
|
151
157
|
|
|
152
158
|
**NOTE**: Make sure to reset the hCaptcha state when you submit your form by calling the method `.resetCaptcha` on your hCaptcha React Component! Passcodes are one-time use, so if your user submits the same passcode twice then it will be rejected by the server the second time.
|
package/dist/index.js
CHANGED
|
@@ -64,12 +64,16 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
64
64
|
|
|
65
65
|
_this.renderCaptcha = _this.renderCaptcha.bind((0, _assertThisInitialized2["default"])(_this));
|
|
66
66
|
_this.resetCaptcha = _this.resetCaptcha.bind((0, _assertThisInitialized2["default"])(_this));
|
|
67
|
-
_this.removeCaptcha = _this.removeCaptcha.bind((0, _assertThisInitialized2["default"])(_this));
|
|
67
|
+
_this.removeCaptcha = _this.removeCaptcha.bind((0, _assertThisInitialized2["default"])(_this));
|
|
68
|
+
_this.isReady = _this.isReady.bind((0, _assertThisInitialized2["default"])(_this)); // Event Handlers
|
|
68
69
|
|
|
69
70
|
_this.handleOnLoad = _this.handleOnLoad.bind((0, _assertThisInitialized2["default"])(_this));
|
|
70
71
|
_this.handleSubmit = _this.handleSubmit.bind((0, _assertThisInitialized2["default"])(_this));
|
|
71
72
|
_this.handleExpire = _this.handleExpire.bind((0, _assertThisInitialized2["default"])(_this));
|
|
72
73
|
_this.handleError = _this.handleError.bind((0, _assertThisInitialized2["default"])(_this));
|
|
74
|
+
_this.handleOpen = _this.handleOpen.bind((0, _assertThisInitialized2["default"])(_this));
|
|
75
|
+
_this.handleClose = _this.handleClose.bind((0, _assertThisInitialized2["default"])(_this));
|
|
76
|
+
_this.handleChallengeExpired = _this.handleChallengeExpired.bind((0, _assertThisInitialized2["default"])(_this));
|
|
73
77
|
var isApiReady = typeof hcaptcha !== 'undefined';
|
|
74
78
|
_this.ref = React.createRef();
|
|
75
79
|
_this.state = {
|
|
@@ -125,11 +129,12 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
125
129
|
}, {
|
|
126
130
|
key: "componentWillUnmount",
|
|
127
131
|
value: function componentWillUnmount() {
|
|
128
|
-
var
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
132
|
+
var captchaId = this.state.captchaId;
|
|
133
|
+
|
|
134
|
+
if (!this.isReady()) {
|
|
135
|
+
return;
|
|
136
|
+
} // Reset any stored variables / timers when unmounting
|
|
137
|
+
|
|
133
138
|
|
|
134
139
|
hcaptcha.reset(captchaId);
|
|
135
140
|
hcaptcha.remove(captchaId);
|
|
@@ -168,7 +173,10 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
168
173
|
var isApiReady = this.state.isApiReady;
|
|
169
174
|
if (!isApiReady) return;
|
|
170
175
|
var renderParams = Object.assign({
|
|
176
|
+
"open-callback": this.handleOpen,
|
|
177
|
+
"close-callback": this.handleClose,
|
|
171
178
|
"error-callback": this.handleError,
|
|
179
|
+
"chalexpired-callback": this.handleChallengeExpired,
|
|
172
180
|
"expired-callback": this.handleExpire,
|
|
173
181
|
"callback": this.handleSubmit
|
|
174
182
|
}, this.props, {
|
|
@@ -187,22 +195,24 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
187
195
|
}, {
|
|
188
196
|
key: "resetCaptcha",
|
|
189
197
|
value: function resetCaptcha() {
|
|
190
|
-
var
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
198
|
+
var captchaId = this.state.captchaId;
|
|
199
|
+
|
|
200
|
+
if (!this.isReady()) {
|
|
201
|
+
return;
|
|
202
|
+
} // Reset captcha state, removes stored token and unticks checkbox
|
|
203
|
+
|
|
195
204
|
|
|
196
205
|
hcaptcha.reset(captchaId);
|
|
197
206
|
}
|
|
198
207
|
}, {
|
|
199
208
|
key: "removeCaptcha",
|
|
200
209
|
value: function removeCaptcha(callback) {
|
|
201
|
-
var
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
210
|
+
var captchaId = this.state.captchaId;
|
|
211
|
+
|
|
212
|
+
if (!this.isReady()) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
206
216
|
this.setState({
|
|
207
217
|
isRemoved: true
|
|
208
218
|
}, function () {
|
|
@@ -230,9 +240,9 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
230
240
|
key: "handleSubmit",
|
|
231
241
|
value: function handleSubmit(event) {
|
|
232
242
|
var onVerify = this.props.onVerify;
|
|
233
|
-
var _this$
|
|
234
|
-
isRemoved = _this$
|
|
235
|
-
captchaId = _this$
|
|
243
|
+
var _this$state = this.state,
|
|
244
|
+
isRemoved = _this$state.isRemoved,
|
|
245
|
+
captchaId = _this$state.captchaId;
|
|
236
246
|
if (typeof hcaptcha === 'undefined' || isRemoved) return;
|
|
237
247
|
var token = hcaptcha.getResponse(captchaId); //Get response token from hCaptcha widget
|
|
238
248
|
|
|
@@ -244,11 +254,12 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
244
254
|
key: "handleExpire",
|
|
245
255
|
value: function handleExpire() {
|
|
246
256
|
var onExpire = this.props.onExpire;
|
|
247
|
-
var
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
257
|
+
var captchaId = this.state.captchaId;
|
|
258
|
+
|
|
259
|
+
if (!this.isReady()) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
252
263
|
hcaptcha.reset(captchaId); // If hCaptcha runs into error, reset captcha - hCaptcha
|
|
253
264
|
|
|
254
265
|
if (onExpire) onExpire();
|
|
@@ -257,24 +268,60 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
257
268
|
key: "handleError",
|
|
258
269
|
value: function handleError(event) {
|
|
259
270
|
var onError = this.props.onError;
|
|
260
|
-
var
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
271
|
+
var captchaId = this.state.captchaId;
|
|
272
|
+
|
|
273
|
+
if (!this.isReady()) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
|
|
265
277
|
hcaptcha.reset(captchaId); // If hCaptcha runs into error, reset captcha - hCaptcha
|
|
266
278
|
|
|
267
279
|
if (onError) onError(event);
|
|
268
280
|
}
|
|
281
|
+
}, {
|
|
282
|
+
key: "isReady",
|
|
283
|
+
value: function isReady() {
|
|
284
|
+
var _this$state2 = this.state,
|
|
285
|
+
isApiReady = _this$state2.isApiReady,
|
|
286
|
+
isRemoved = _this$state2.isRemoved;
|
|
287
|
+
return isApiReady && !isRemoved;
|
|
288
|
+
}
|
|
289
|
+
}, {
|
|
290
|
+
key: "handleOpen",
|
|
291
|
+
value: function handleOpen() {
|
|
292
|
+
if (!this.isReady() || !this.props.onOpen) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
this.props.onOpen();
|
|
297
|
+
}
|
|
298
|
+
}, {
|
|
299
|
+
key: "handleClose",
|
|
300
|
+
value: function handleClose() {
|
|
301
|
+
if (!this.isReady() || !this.props.onClose) {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
this.props.onClose();
|
|
306
|
+
}
|
|
307
|
+
}, {
|
|
308
|
+
key: "handleChallengeExpired",
|
|
309
|
+
value: function handleChallengeExpired() {
|
|
310
|
+
if (!this.isReady() || !this.props.onChalExpired) {
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
this.props.onChalExpired();
|
|
315
|
+
}
|
|
269
316
|
}, {
|
|
270
317
|
key: "execute",
|
|
271
318
|
value: function execute() {
|
|
272
319
|
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
273
|
-
var
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
320
|
+
var captchaId = this.state.captchaId;
|
|
321
|
+
|
|
322
|
+
if (!this.isReady()) {
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
278
325
|
|
|
279
326
|
if (opts && (0, _typeof2["default"])(opts) !== "object") {
|
|
280
327
|
opts = null;
|
|
@@ -282,6 +329,31 @@ var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
|
282
329
|
|
|
283
330
|
return hcaptcha.execute(captchaId, opts);
|
|
284
331
|
}
|
|
332
|
+
}, {
|
|
333
|
+
key: "setData",
|
|
334
|
+
value: function setData(data) {
|
|
335
|
+
var captchaId = this.state.captchaId;
|
|
336
|
+
|
|
337
|
+
if (!this.isReady()) {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (data && (0, _typeof2["default"])(data) !== "object") {
|
|
342
|
+
data = null;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
hcaptcha.setData(captchaId, data);
|
|
346
|
+
}
|
|
347
|
+
}, {
|
|
348
|
+
key: "getResponse",
|
|
349
|
+
value: function getResponse() {
|
|
350
|
+
return hcaptcha.getResponse(this.state.captchaId);
|
|
351
|
+
}
|
|
352
|
+
}, {
|
|
353
|
+
key: "getRespKey",
|
|
354
|
+
value: function getRespKey() {
|
|
355
|
+
return hcaptcha.getRespKey(this.state.captchaId);
|
|
356
|
+
}
|
|
285
357
|
}, {
|
|
286
358
|
key: "render",
|
|
287
359
|
value: function render() {
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -39,12 +39,16 @@ class HCaptcha extends React.Component {
|
|
|
39
39
|
this.renderCaptcha = this.renderCaptcha.bind(this);
|
|
40
40
|
this.resetCaptcha = this.resetCaptcha.bind(this);
|
|
41
41
|
this.removeCaptcha = this.removeCaptcha.bind(this);
|
|
42
|
+
this.isReady = this.isReady.bind(this);
|
|
42
43
|
|
|
43
44
|
// Event Handlers
|
|
44
45
|
this.handleOnLoad = this.handleOnLoad.bind(this);
|
|
45
46
|
this.handleSubmit = this.handleSubmit.bind(this);
|
|
46
47
|
this.handleExpire = this.handleExpire.bind(this);
|
|
47
48
|
this.handleError = this.handleError.bind(this);
|
|
49
|
+
this.handleOpen = this.handleOpen.bind(this);
|
|
50
|
+
this.handleClose = this.handleClose.bind(this);
|
|
51
|
+
this.handleChallengeExpired = this.handleChallengeExpired.bind(this);
|
|
48
52
|
|
|
49
53
|
const isApiReady = typeof hcaptcha !== 'undefined';
|
|
50
54
|
|
|
@@ -88,8 +92,11 @@ class HCaptcha extends React.Component {
|
|
|
88
92
|
}
|
|
89
93
|
|
|
90
94
|
componentWillUnmount() {
|
|
91
|
-
const {
|
|
92
|
-
|
|
95
|
+
const { captchaId } = this.state;
|
|
96
|
+
|
|
97
|
+
if (!this.isReady()) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
93
100
|
|
|
94
101
|
// Reset any stored variables / timers when unmounting
|
|
95
102
|
hcaptcha.reset(captchaId);
|
|
@@ -124,7 +131,10 @@ class HCaptcha extends React.Component {
|
|
|
124
131
|
if (!isApiReady) return;
|
|
125
132
|
|
|
126
133
|
const renderParams = Object.assign({
|
|
134
|
+
"open-callback" : this.handleOpen,
|
|
135
|
+
"close-callback" : this.handleClose,
|
|
127
136
|
"error-callback" : this.handleError,
|
|
137
|
+
"chalexpired-callback": this.handleChallengeExpired,
|
|
128
138
|
"expired-callback" : this.handleExpire,
|
|
129
139
|
"callback" : this.handleSubmit,
|
|
130
140
|
}, this.props, {
|
|
@@ -141,17 +151,21 @@ class HCaptcha extends React.Component {
|
|
|
141
151
|
}
|
|
142
152
|
|
|
143
153
|
resetCaptcha() {
|
|
144
|
-
const {
|
|
154
|
+
const { captchaId } = this.state;
|
|
145
155
|
|
|
146
|
-
if (!
|
|
156
|
+
if (!this.isReady()) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
147
159
|
// Reset captcha state, removes stored token and unticks checkbox
|
|
148
160
|
hcaptcha.reset(captchaId)
|
|
149
161
|
}
|
|
150
162
|
|
|
151
163
|
removeCaptcha(callback) {
|
|
152
|
-
const {
|
|
164
|
+
const { captchaId } = this.state;
|
|
153
165
|
|
|
154
|
-
if (!
|
|
166
|
+
if (!this.isReady()) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
155
169
|
|
|
156
170
|
this.setState({ isRemoved: true }, () => {
|
|
157
171
|
hcaptcha.remove(captchaId);
|
|
@@ -184,9 +198,12 @@ class HCaptcha extends React.Component {
|
|
|
184
198
|
|
|
185
199
|
handleExpire () {
|
|
186
200
|
const { onExpire } = this.props;
|
|
187
|
-
const {
|
|
201
|
+
const { captchaId } = this.state;
|
|
202
|
+
|
|
203
|
+
if (!this.isReady()) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
188
206
|
|
|
189
|
-
if (!isApiReady || isRemoved) return
|
|
190
207
|
hcaptcha.reset(captchaId) // If hCaptcha runs into error, reset captcha - hCaptcha
|
|
191
208
|
|
|
192
209
|
if (onExpire) onExpire();
|
|
@@ -194,18 +211,52 @@ class HCaptcha extends React.Component {
|
|
|
194
211
|
|
|
195
212
|
handleError (event) {
|
|
196
213
|
const { onError } = this.props;
|
|
197
|
-
const {
|
|
214
|
+
const { captchaId } = this.state;
|
|
198
215
|
|
|
199
|
-
if (!
|
|
216
|
+
if (!this.isReady()) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
200
219
|
|
|
201
220
|
hcaptcha.reset(captchaId) // If hCaptcha runs into error, reset captcha - hCaptcha
|
|
202
221
|
if (onError) onError(event);
|
|
203
222
|
}
|
|
204
223
|
|
|
224
|
+
isReady () {
|
|
225
|
+
const { isApiReady, isRemoved } = this.state;
|
|
226
|
+
|
|
227
|
+
return isApiReady && !isRemoved;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
handleOpen () {
|
|
231
|
+
if (!this.isReady() || !this.props.onOpen) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
this.props.onOpen();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
handleClose () {
|
|
239
|
+
if (!this.isReady() || !this.props.onClose) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
this.props.onClose();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
handleChallengeExpired () {
|
|
247
|
+
if (!this.isReady() || !this.props.onChalExpired) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
this.props.onChalExpired();
|
|
252
|
+
}
|
|
253
|
+
|
|
205
254
|
execute (opts = null) {
|
|
206
|
-
const {
|
|
255
|
+
const { captchaId } = this.state;
|
|
207
256
|
|
|
208
|
-
if (!
|
|
257
|
+
if (!this.isReady()) {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
209
260
|
|
|
210
261
|
if (opts && typeof opts !== "object") {
|
|
211
262
|
opts = null;
|
|
@@ -214,6 +265,28 @@ class HCaptcha extends React.Component {
|
|
|
214
265
|
return hcaptcha.execute(captchaId, opts);
|
|
215
266
|
}
|
|
216
267
|
|
|
268
|
+
setData (data) {
|
|
269
|
+
const { captchaId } = this.state;
|
|
270
|
+
|
|
271
|
+
if (!this.isReady()) {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (data && typeof data !== "object") {
|
|
276
|
+
data = null;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
hcaptcha.setData(captchaId, data);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
getResponse() {
|
|
283
|
+
return hcaptcha.getResponse(this.state.captchaId);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
getRespKey() {
|
|
287
|
+
return hcaptcha.getRespKey(this.state.captchaId)
|
|
288
|
+
}
|
|
289
|
+
|
|
217
290
|
render () {
|
|
218
291
|
const { elementId } = this.state;
|
|
219
292
|
return <div ref={this.ref} id={elementId}></div>
|
package/types/index.d.ts
CHANGED
|
@@ -15,8 +15,11 @@ interface HCaptchaState {
|
|
|
15
15
|
|
|
16
16
|
interface HCaptchaProps {
|
|
17
17
|
onExpire?: () => any;
|
|
18
|
+
onOpen?: () => any;
|
|
19
|
+
onClose?: () => any;
|
|
20
|
+
onChalExpired?: () => any;
|
|
18
21
|
onError?: (event: string) => any;
|
|
19
|
-
onVerify?: (token: string) => any;
|
|
22
|
+
onVerify?: (token: string, ekey: string) => any;
|
|
20
23
|
onLoad?: () => any;
|
|
21
24
|
languageOverride?: string;
|
|
22
25
|
sitekey: string;
|
|
@@ -36,7 +39,10 @@ declare class HCaptcha extends React.Component<HCaptchaProps, HCaptchaState> {
|
|
|
36
39
|
resetCaptcha(): void;
|
|
37
40
|
renderCaptcha(): void;
|
|
38
41
|
removeCaptcha(): void;
|
|
39
|
-
|
|
42
|
+
getRespKey(): string;
|
|
43
|
+
getResponse(): string;
|
|
44
|
+
setData(data: object): void;
|
|
45
|
+
execute(opts: { async: true }): Promise<ExecuteResponse>;
|
|
40
46
|
execute(opts?: { async: false }): void;
|
|
41
47
|
execute(opts?: { async: boolean }): Promise<ExecuteResponse> | void;
|
|
42
48
|
}
|