@hcaptcha/react-hcaptcha 1.4.0 → 1.4.1
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/dist/esm/index.js +332 -0
- package/dist/esm/utils.js +14 -0
- package/package.json +2 -2
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
|
|
2
|
+
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { generateQuery } from "./utils.js"; // Create script to init hCaptcha
|
|
5
|
+
|
|
6
|
+
var onLoadListeners = [];
|
|
7
|
+
var apiScriptRequested = false; // Generate hCaptcha API Script
|
|
8
|
+
|
|
9
|
+
var mountCaptchaScript = function mountCaptchaScript(params) {
|
|
10
|
+
if (params === void 0) {
|
|
11
|
+
params = {};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
apiScriptRequested = true; // Create global onload callback
|
|
15
|
+
|
|
16
|
+
window.hcaptchaOnLoad = function () {
|
|
17
|
+
// Iterate over onload listeners, call each listener
|
|
18
|
+
onLoadListeners = onLoadListeners.filter(function (listener) {
|
|
19
|
+
listener();
|
|
20
|
+
return false;
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
var domain = params.apihost || "https://js.hcaptcha.com";
|
|
25
|
+
delete params.apihost;
|
|
26
|
+
var script = document.createElement("script");
|
|
27
|
+
script.src = domain + "/1/api.js?render=explicit&onload=hcaptchaOnLoad";
|
|
28
|
+
script.async = true;
|
|
29
|
+
var query = generateQuery(params);
|
|
30
|
+
script.src += query !== "" ? "&" + query : "";
|
|
31
|
+
document.head.appendChild(script);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
var HCaptcha = /*#__PURE__*/function (_React$Component) {
|
|
35
|
+
_inheritsLoose(HCaptcha, _React$Component);
|
|
36
|
+
|
|
37
|
+
function HCaptcha(props) {
|
|
38
|
+
var _this;
|
|
39
|
+
|
|
40
|
+
_this = _React$Component.call(this, props) || this; // API Methods
|
|
41
|
+
|
|
42
|
+
_this.renderCaptcha = _this.renderCaptcha.bind(_assertThisInitialized(_this));
|
|
43
|
+
_this.resetCaptcha = _this.resetCaptcha.bind(_assertThisInitialized(_this));
|
|
44
|
+
_this.removeCaptcha = _this.removeCaptcha.bind(_assertThisInitialized(_this));
|
|
45
|
+
_this.isReady = _this.isReady.bind(_assertThisInitialized(_this)); // Event Handlers
|
|
46
|
+
|
|
47
|
+
_this.handleOnLoad = _this.handleOnLoad.bind(_assertThisInitialized(_this));
|
|
48
|
+
_this.handleSubmit = _this.handleSubmit.bind(_assertThisInitialized(_this));
|
|
49
|
+
_this.handleExpire = _this.handleExpire.bind(_assertThisInitialized(_this));
|
|
50
|
+
_this.handleError = _this.handleError.bind(_assertThisInitialized(_this));
|
|
51
|
+
_this.handleOpen = _this.handleOpen.bind(_assertThisInitialized(_this));
|
|
52
|
+
_this.handleClose = _this.handleClose.bind(_assertThisInitialized(_this));
|
|
53
|
+
_this.handleChallengeExpired = _this.handleChallengeExpired.bind(_assertThisInitialized(_this));
|
|
54
|
+
var isApiReady = typeof hcaptcha !== 'undefined';
|
|
55
|
+
_this.ref = /*#__PURE__*/React.createRef();
|
|
56
|
+
_this.state = {
|
|
57
|
+
isApiReady: isApiReady,
|
|
58
|
+
isRemoved: false,
|
|
59
|
+
elementId: props.id,
|
|
60
|
+
captchaId: ''
|
|
61
|
+
};
|
|
62
|
+
return _this;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
var _proto = HCaptcha.prototype;
|
|
66
|
+
|
|
67
|
+
_proto.componentDidMount = function componentDidMount() {
|
|
68
|
+
//Once captcha is mounted intialize hCaptcha - hCaptcha
|
|
69
|
+
var _this$props = this.props,
|
|
70
|
+
apihost = _this$props.apihost,
|
|
71
|
+
assethost = _this$props.assethost,
|
|
72
|
+
endpoint = _this$props.endpoint,
|
|
73
|
+
host = _this$props.host,
|
|
74
|
+
imghost = _this$props.imghost,
|
|
75
|
+
hl = _this$props.languageOverride,
|
|
76
|
+
reCaptchaCompat = _this$props.reCaptchaCompat,
|
|
77
|
+
reportapi = _this$props.reportapi,
|
|
78
|
+
sentry = _this$props.sentry,
|
|
79
|
+
custom = _this$props.custom;
|
|
80
|
+
var isApiReady = this.state.isApiReady;
|
|
81
|
+
|
|
82
|
+
if (!isApiReady) {
|
|
83
|
+
//Check if hCaptcha has already been loaded, if not create script tag and wait to render captcha
|
|
84
|
+
if (apiScriptRequested) {
|
|
85
|
+
return;
|
|
86
|
+
} // Only create the script tag once, use a global variable to track
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
mountCaptchaScript({
|
|
90
|
+
apihost: apihost,
|
|
91
|
+
assethost: assethost,
|
|
92
|
+
endpoint: endpoint,
|
|
93
|
+
hl: hl,
|
|
94
|
+
host: host,
|
|
95
|
+
imghost: imghost,
|
|
96
|
+
recaptchacompat: reCaptchaCompat === false ? "off" : null,
|
|
97
|
+
reportapi: reportapi,
|
|
98
|
+
sentry: sentry,
|
|
99
|
+
custom: custom
|
|
100
|
+
}); // Add onload callback to global onload listeners
|
|
101
|
+
|
|
102
|
+
onLoadListeners.push(this.handleOnLoad);
|
|
103
|
+
} else {
|
|
104
|
+
this.renderCaptcha();
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
_proto.componentWillUnmount = function componentWillUnmount() {
|
|
109
|
+
var captchaId = this.state.captchaId;
|
|
110
|
+
|
|
111
|
+
if (!this.isReady()) {
|
|
112
|
+
return;
|
|
113
|
+
} // Reset any stored variables / timers when unmounting
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
hcaptcha.reset(captchaId);
|
|
117
|
+
hcaptcha.remove(captchaId);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
_proto.shouldComponentUpdate = function shouldComponentUpdate(nextProps, nextState) {
|
|
121
|
+
// Prevent component re-rendering when these internal state variables are updated
|
|
122
|
+
if (this.state.isApiReady !== nextState.isApiReady || this.state.isRemoved !== nextState.isRemoved) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return true;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
_proto.componentDidUpdate = function componentDidUpdate(prevProps) {
|
|
130
|
+
var _this2 = this;
|
|
131
|
+
|
|
132
|
+
// Prop Keys that could change
|
|
133
|
+
var keys = ['sitekey', 'size', 'theme', 'tabindex', 'languageOverride', 'endpoint']; // See if any props changed during component update
|
|
134
|
+
|
|
135
|
+
var match = keys.every(function (key) {
|
|
136
|
+
return prevProps[key] === _this2.props[key];
|
|
137
|
+
}); // If they have changed, remove current captcha and render a new one
|
|
138
|
+
|
|
139
|
+
if (!match) {
|
|
140
|
+
this.removeCaptcha(function () {
|
|
141
|
+
_this2.renderCaptcha();
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
_proto.renderCaptcha = function renderCaptcha(onReady) {
|
|
147
|
+
var isApiReady = this.state.isApiReady;
|
|
148
|
+
if (!isApiReady) return;
|
|
149
|
+
var renderParams = Object.assign({
|
|
150
|
+
"open-callback": this.handleOpen,
|
|
151
|
+
"close-callback": this.handleClose,
|
|
152
|
+
"error-callback": this.handleError,
|
|
153
|
+
"chalexpired-callback": this.handleChallengeExpired,
|
|
154
|
+
"expired-callback": this.handleExpire,
|
|
155
|
+
"callback": this.handleSubmit
|
|
156
|
+
}, this.props, {
|
|
157
|
+
hl: this.props.hl || this.props.languageOverride,
|
|
158
|
+
languageOverride: undefined
|
|
159
|
+
}); //Render hCaptcha widget and provide necessary callbacks - hCaptcha
|
|
160
|
+
|
|
161
|
+
var captchaId = hcaptcha.render(this.ref.current, renderParams);
|
|
162
|
+
this.setState({
|
|
163
|
+
isRemoved: false,
|
|
164
|
+
captchaId: captchaId
|
|
165
|
+
}, function () {
|
|
166
|
+
onReady && onReady();
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
_proto.resetCaptcha = function resetCaptcha() {
|
|
171
|
+
var captchaId = this.state.captchaId;
|
|
172
|
+
|
|
173
|
+
if (!this.isReady()) {
|
|
174
|
+
return;
|
|
175
|
+
} // Reset captcha state, removes stored token and unticks checkbox
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
hcaptcha.reset(captchaId);
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
_proto.removeCaptcha = function removeCaptcha(callback) {
|
|
182
|
+
var captchaId = this.state.captchaId;
|
|
183
|
+
|
|
184
|
+
if (!this.isReady()) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
this.setState({
|
|
189
|
+
isRemoved: true
|
|
190
|
+
}, function () {
|
|
191
|
+
hcaptcha.remove(captchaId);
|
|
192
|
+
callback && callback();
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
_proto.handleOnLoad = function handleOnLoad() {
|
|
197
|
+
var _this3 = this;
|
|
198
|
+
|
|
199
|
+
this.setState({
|
|
200
|
+
isApiReady: true
|
|
201
|
+
}, function () {
|
|
202
|
+
// render captcha and wait for captcha id
|
|
203
|
+
_this3.renderCaptcha(function () {
|
|
204
|
+
// trigger onLoad if it exists
|
|
205
|
+
var onLoad = _this3.props.onLoad;
|
|
206
|
+
if (onLoad) onLoad();
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
_proto.handleSubmit = function handleSubmit(event) {
|
|
212
|
+
var onVerify = this.props.onVerify;
|
|
213
|
+
var _this$state = this.state,
|
|
214
|
+
isRemoved = _this$state.isRemoved,
|
|
215
|
+
captchaId = _this$state.captchaId;
|
|
216
|
+
if (typeof hcaptcha === 'undefined' || isRemoved) return;
|
|
217
|
+
var token = hcaptcha.getResponse(captchaId); //Get response token from hCaptcha widget
|
|
218
|
+
|
|
219
|
+
var ekey = hcaptcha.getRespKey(captchaId); //Get current challenge session id from hCaptcha widget
|
|
220
|
+
|
|
221
|
+
onVerify(token, ekey); //Dispatch event to verify user response
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
_proto.handleExpire = function handleExpire() {
|
|
225
|
+
var onExpire = this.props.onExpire;
|
|
226
|
+
var captchaId = this.state.captchaId;
|
|
227
|
+
|
|
228
|
+
if (!this.isReady()) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
hcaptcha.reset(captchaId); // If hCaptcha runs into error, reset captcha - hCaptcha
|
|
233
|
+
|
|
234
|
+
if (onExpire) onExpire();
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
_proto.handleError = function handleError(event) {
|
|
238
|
+
var onError = this.props.onError;
|
|
239
|
+
var captchaId = this.state.captchaId;
|
|
240
|
+
|
|
241
|
+
if (!this.isReady()) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
hcaptcha.reset(captchaId); // If hCaptcha runs into error, reset captcha - hCaptcha
|
|
246
|
+
|
|
247
|
+
if (onError) onError(event);
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
_proto.isReady = function isReady() {
|
|
251
|
+
var _this$state2 = this.state,
|
|
252
|
+
isApiReady = _this$state2.isApiReady,
|
|
253
|
+
isRemoved = _this$state2.isRemoved;
|
|
254
|
+
return isApiReady && !isRemoved;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
_proto.handleOpen = function handleOpen() {
|
|
258
|
+
if (!this.isReady() || !this.props.onOpen) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
this.props.onOpen();
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
_proto.handleClose = function handleClose() {
|
|
266
|
+
if (!this.isReady() || !this.props.onClose) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
this.props.onClose();
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
_proto.handleChallengeExpired = function handleChallengeExpired() {
|
|
274
|
+
if (!this.isReady() || !this.props.onChalExpired) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
this.props.onChalExpired();
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
_proto.execute = function execute(opts) {
|
|
282
|
+
if (opts === void 0) {
|
|
283
|
+
opts = null;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
var captchaId = this.state.captchaId;
|
|
287
|
+
|
|
288
|
+
if (!this.isReady()) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (opts && typeof opts !== "object") {
|
|
293
|
+
opts = null;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return hcaptcha.execute(captchaId, opts);
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
_proto.setData = function setData(data) {
|
|
300
|
+
var captchaId = this.state.captchaId;
|
|
301
|
+
|
|
302
|
+
if (!this.isReady()) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (data && typeof data !== "object") {
|
|
307
|
+
data = null;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
hcaptcha.setData(captchaId, data);
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
_proto.getResponse = function getResponse() {
|
|
314
|
+
return hcaptcha.getResponse(this.state.captchaId);
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
_proto.getRespKey = function getRespKey() {
|
|
318
|
+
return hcaptcha.getRespKey(this.state.captchaId);
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
_proto.render = function render() {
|
|
322
|
+
var elementId = this.state.elementId;
|
|
323
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
324
|
+
ref: this.ref,
|
|
325
|
+
id: elementId
|
|
326
|
+
});
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
return HCaptcha;
|
|
330
|
+
}(React.Component);
|
|
331
|
+
|
|
332
|
+
export default HCaptcha;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
function generateQuery(params) {
|
|
2
|
+
return Object.entries(params).filter(function (_ref) {
|
|
3
|
+
var key = _ref[0],
|
|
4
|
+
value = _ref[1];
|
|
5
|
+
return value || value === false;
|
|
6
|
+
}).map(function (_ref2) {
|
|
7
|
+
var key = _ref2[0],
|
|
8
|
+
value = _ref2[1];
|
|
9
|
+
return encodeURIComponent(key) + "=" + encodeURIComponent(value);
|
|
10
|
+
}).join("&");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
;
|
|
14
|
+
export { generateQuery };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hcaptcha/react-hcaptcha",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"types": "types/index.d.ts",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"prebuild": "rimraf dist",
|
|
19
19
|
"build": "npm run transpile && npm run build:esm",
|
|
20
20
|
"build:esm": "cross-env BABEL_ENV=esm babel src -d dist/esm --copy-files",
|
|
21
|
-
"prepublishOnly": "npm run
|
|
21
|
+
"prepublishOnly": "npm run build"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
24
|
"react": ">= 16.3.0",
|