@hcaptcha/react-hcaptcha 1.16.0 → 1.17.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 +52 -0
- package/dist/esm/hooks/Context.js +2 -0
- package/dist/esm/hooks/Provider.js +120 -0
- package/dist/esm/hooks/index.js +2 -0
- package/dist/esm/hooks/useHCaptcha.tsx +4 -0
- package/dist/hooks/Context.js +8 -0
- package/dist/hooks/Provider.js +137 -0
- package/dist/hooks/index.js +19 -0
- package/dist/hooks/useHCaptcha.tsx +4 -0
- package/package.json +11 -1
- package/src/hooks/Context.jsx +3 -0
- package/src/hooks/Provider.jsx +98 -0
- package/src/hooks/index.jsx +2 -0
- package/src/hooks/useHCaptcha.tsx +4 -0
package/README.md
CHANGED
|
@@ -93,6 +93,58 @@ import { default as RenamedCaptcha } from '../utils/captcha';
|
|
|
93
93
|
</FormComponent>
|
|
94
94
|
```
|
|
95
95
|
|
|
96
|
+
#### Provider/Hook Pattern
|
|
97
|
+
|
|
98
|
+
You can also use the Provider/Hook pattern:
|
|
99
|
+
|
|
100
|
+
```js
|
|
101
|
+
import { HCaptchaProvider, useHCaptcha } from '@hcaptcha/react-hcaptcha/hooks';
|
|
102
|
+
|
|
103
|
+
function App() {
|
|
104
|
+
return (
|
|
105
|
+
<HCaptchaProvider sitekey="your-sitekey">
|
|
106
|
+
<Form />
|
|
107
|
+
</HCaptchaProvider>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function Form() {
|
|
112
|
+
const { ready, token, executeInstance } = useHCaptcha();
|
|
113
|
+
|
|
114
|
+
const onSubmit = async () => {
|
|
115
|
+
const response = await executeInstance();
|
|
116
|
+
console.log("Token:", response);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
return <button onClick={onSubmit} disabled={!ready}>Submit</button>;
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
##### Provider Props
|
|
124
|
+
|
|
125
|
+
|Name|Values/Type|Required|Default|Description|
|
|
126
|
+
|---|---|---|---|---|
|
|
127
|
+
|`sitekey`|String|**Yes**|`-`|Your hCaptcha sitekey.|
|
|
128
|
+
|`size`|String (normal, compact, invisible)|No|`normal`|The size of the hCaptcha widget.|
|
|
129
|
+
|`theme`|String (light, dark)|No|`light`|The theme of the widget.|
|
|
130
|
+
|`rqdata`|String|No|`-`|See enterprise docs.|
|
|
131
|
+
|`languageOverride`|String|No|`-`|Language override (ISO 639-2 code).|
|
|
132
|
+
|`onVerify`|Function|No|`-`|Callback when captcha is verified.|
|
|
133
|
+
|`onError`|Function|No|`-`|Callback when an error occurs.|
|
|
134
|
+
|
|
135
|
+
##### useHCaptcha Hook
|
|
136
|
+
|
|
137
|
+
Returns an object with:
|
|
138
|
+
|
|
139
|
+
|Property|Type|Description|
|
|
140
|
+
|---|---|---|
|
|
141
|
+
|`executeInstance`|Function|Execute hCaptcha programmatically. Returns a Promise that resolve with a new token.|
|
|
142
|
+
|`resetInstance`|Function|Reset the hCaptcha client instance.|
|
|
143
|
+
|`ready`|Boolean|Whether hCaptcha is loaded and ready.|
|
|
144
|
+
|`token`|String \| null|Current hCaptcha token (if verified).|
|
|
145
|
+
|`error`|Error \| null|Any error that occurred.|
|
|
146
|
+
|`sitekey`|String|The hCaptcha sitekey.|
|
|
147
|
+
|
|
96
148
|
#### Advanced
|
|
97
149
|
|
|
98
150
|
In most real-world implementations, you'll probably be using a form library such as [Formik](https://github.com/jaredpalmer/formik) or [React Hook Form](https://github.com/react-hook-form/react-hook-form).
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/extends";
|
|
2
|
+
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
|
|
3
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
4
|
+
import React, { useEffect, useRef, useState } from "react";
|
|
5
|
+
import HCaptcha from "../index.js";
|
|
6
|
+
import { HCaptchaContext } from "./Context";
|
|
7
|
+
export var HCaptchaProvider = function HCaptchaProvider(_ref) {
|
|
8
|
+
var _ref$sitekey = _ref.sitekey,
|
|
9
|
+
sitekey = _ref$sitekey === void 0 ? null : _ref$sitekey,
|
|
10
|
+
_ref$size = _ref.size,
|
|
11
|
+
size = _ref$size === void 0 ? "normal" : _ref$size,
|
|
12
|
+
_ref$theme = _ref.theme,
|
|
13
|
+
theme = _ref$theme === void 0 ? "light" : _ref$theme,
|
|
14
|
+
_ref$rqdata = _ref.rqdata,
|
|
15
|
+
rqdata = _ref$rqdata === void 0 ? null : _ref$rqdata,
|
|
16
|
+
_ref$languageOverride = _ref.languageOverride,
|
|
17
|
+
languageOverride = _ref$languageOverride === void 0 ? null : _ref$languageOverride,
|
|
18
|
+
onVerify = _ref.onVerify,
|
|
19
|
+
onError = _ref.onError,
|
|
20
|
+
children = _ref.children;
|
|
21
|
+
var hcaptchaRef = useRef(null);
|
|
22
|
+
var _useState = useState(false),
|
|
23
|
+
ready = _useState[0],
|
|
24
|
+
setReady = _useState[1];
|
|
25
|
+
var _useState2 = useState(null),
|
|
26
|
+
token = _useState2[0],
|
|
27
|
+
setToken = _useState2[1];
|
|
28
|
+
var _useState3 = useState(null),
|
|
29
|
+
error = _useState3[0],
|
|
30
|
+
setError = _useState3[1];
|
|
31
|
+
var handleReady = function handleReady() {
|
|
32
|
+
setReady(true);
|
|
33
|
+
};
|
|
34
|
+
var handleError = function handleError(error) {
|
|
35
|
+
setError(error);
|
|
36
|
+
onError && onError(error);
|
|
37
|
+
};
|
|
38
|
+
var handleExpire = function handleExpire() {
|
|
39
|
+
setToken(null);
|
|
40
|
+
};
|
|
41
|
+
var handleVerify = function handleVerify(token) {
|
|
42
|
+
setToken(token);
|
|
43
|
+
onVerify && onVerify(token);
|
|
44
|
+
};
|
|
45
|
+
var executeInstance = /*#__PURE__*/function () {
|
|
46
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(config) {
|
|
47
|
+
var _yield$hcaptchaRef$cu, response;
|
|
48
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
49
|
+
while (1) switch (_context.prev = _context.next) {
|
|
50
|
+
case 0:
|
|
51
|
+
if (config === void 0) {
|
|
52
|
+
config = {};
|
|
53
|
+
}
|
|
54
|
+
_context.prev = 1;
|
|
55
|
+
if (ready) {
|
|
56
|
+
_context.next = 4;
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
throw new Error("hCaptcha not ready");
|
|
60
|
+
case 4:
|
|
61
|
+
if (token) {
|
|
62
|
+
resetInstance();
|
|
63
|
+
}
|
|
64
|
+
_context.next = 7;
|
|
65
|
+
return hcaptchaRef.current.execute(_extends({
|
|
66
|
+
async: true
|
|
67
|
+
}, config.rqdata ? {
|
|
68
|
+
rqdata: config.rqdata
|
|
69
|
+
} : {}));
|
|
70
|
+
case 7:
|
|
71
|
+
_yield$hcaptchaRef$cu = _context.sent;
|
|
72
|
+
response = _yield$hcaptchaRef$cu.response;
|
|
73
|
+
setToken(response);
|
|
74
|
+
return _context.abrupt("return", response);
|
|
75
|
+
case 13:
|
|
76
|
+
_context.prev = 13;
|
|
77
|
+
_context.t0 = _context["catch"](1);
|
|
78
|
+
setError(_context.t0);
|
|
79
|
+
onError && onError(_context.t0);
|
|
80
|
+
case 17:
|
|
81
|
+
case "end":
|
|
82
|
+
return _context.stop();
|
|
83
|
+
}
|
|
84
|
+
}, _callee, null, [[1, 13]]);
|
|
85
|
+
}));
|
|
86
|
+
return function executeInstance(_x) {
|
|
87
|
+
return _ref2.apply(this, arguments);
|
|
88
|
+
};
|
|
89
|
+
}();
|
|
90
|
+
var resetInstance = function resetInstance() {
|
|
91
|
+
var _hcaptchaRef$current;
|
|
92
|
+
hcaptchaRef == null || (_hcaptchaRef$current = hcaptchaRef.current) == null || _hcaptchaRef$current.resetCaptcha();
|
|
93
|
+
};
|
|
94
|
+
useEffect(function () {
|
|
95
|
+
if (rqdata) {
|
|
96
|
+
var _hcaptchaRef$current2;
|
|
97
|
+
hcaptchaRef == null || (_hcaptchaRef$current2 = hcaptchaRef.current) == null || _hcaptchaRef$current2.setData(rqdata);
|
|
98
|
+
}
|
|
99
|
+
}, [rqdata]);
|
|
100
|
+
return /*#__PURE__*/React.createElement(HCaptchaContext.Provider, {
|
|
101
|
+
value: {
|
|
102
|
+
sitekey: sitekey,
|
|
103
|
+
error: error,
|
|
104
|
+
token: token,
|
|
105
|
+
ready: ready,
|
|
106
|
+
executeInstance: executeInstance,
|
|
107
|
+
resetInstance: resetInstance
|
|
108
|
+
}
|
|
109
|
+
}, children, /*#__PURE__*/React.createElement(HCaptcha, {
|
|
110
|
+
sitekey: sitekey,
|
|
111
|
+
size: size,
|
|
112
|
+
theme: theme,
|
|
113
|
+
languageOverride: languageOverride,
|
|
114
|
+
onReady: handleReady,
|
|
115
|
+
onVerify: handleVerify,
|
|
116
|
+
onExpire: handleExpire,
|
|
117
|
+
onError: handleError,
|
|
118
|
+
ref: hcaptchaRef
|
|
119
|
+
}));
|
|
120
|
+
};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
var _typeof = require("@babel/runtime/helpers/typeof");
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.HCaptchaProvider = void 0;
|
|
9
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
12
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
13
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
14
|
+
var _index = _interopRequireDefault(require("../index.js"));
|
|
15
|
+
var _Context = require("./Context");
|
|
16
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
|
|
17
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
|
|
18
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
19
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
20
|
+
var HCaptchaProvider = exports.HCaptchaProvider = function HCaptchaProvider(_ref) {
|
|
21
|
+
var _ref$sitekey = _ref.sitekey,
|
|
22
|
+
sitekey = _ref$sitekey === void 0 ? null : _ref$sitekey,
|
|
23
|
+
_ref$size = _ref.size,
|
|
24
|
+
size = _ref$size === void 0 ? "normal" : _ref$size,
|
|
25
|
+
_ref$theme = _ref.theme,
|
|
26
|
+
theme = _ref$theme === void 0 ? "light" : _ref$theme,
|
|
27
|
+
_ref$rqdata = _ref.rqdata,
|
|
28
|
+
rqdata = _ref$rqdata === void 0 ? null : _ref$rqdata,
|
|
29
|
+
_ref$languageOverride = _ref.languageOverride,
|
|
30
|
+
languageOverride = _ref$languageOverride === void 0 ? null : _ref$languageOverride,
|
|
31
|
+
onVerify = _ref.onVerify,
|
|
32
|
+
onError = _ref.onError,
|
|
33
|
+
children = _ref.children;
|
|
34
|
+
var hcaptchaRef = (0, _react.useRef)(null);
|
|
35
|
+
var _useState = (0, _react.useState)(false),
|
|
36
|
+
_useState2 = (0, _slicedToArray2["default"])(_useState, 2),
|
|
37
|
+
ready = _useState2[0],
|
|
38
|
+
setReady = _useState2[1];
|
|
39
|
+
var _useState3 = (0, _react.useState)(null),
|
|
40
|
+
_useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
|
|
41
|
+
token = _useState4[0],
|
|
42
|
+
setToken = _useState4[1];
|
|
43
|
+
var _useState5 = (0, _react.useState)(null),
|
|
44
|
+
_useState6 = (0, _slicedToArray2["default"])(_useState5, 2),
|
|
45
|
+
error = _useState6[0],
|
|
46
|
+
setError = _useState6[1];
|
|
47
|
+
var handleReady = function handleReady() {
|
|
48
|
+
setReady(true);
|
|
49
|
+
};
|
|
50
|
+
var handleError = function handleError(error) {
|
|
51
|
+
setError(error);
|
|
52
|
+
onError && onError(error);
|
|
53
|
+
};
|
|
54
|
+
var handleExpire = function handleExpire() {
|
|
55
|
+
setToken(null);
|
|
56
|
+
};
|
|
57
|
+
var handleVerify = function handleVerify(token) {
|
|
58
|
+
setToken(token);
|
|
59
|
+
onVerify && onVerify(token);
|
|
60
|
+
};
|
|
61
|
+
var executeInstance = /*#__PURE__*/function () {
|
|
62
|
+
var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
|
|
63
|
+
var config,
|
|
64
|
+
_yield$hcaptchaRef$cu,
|
|
65
|
+
response,
|
|
66
|
+
_args = arguments;
|
|
67
|
+
return _regenerator["default"].wrap(function _callee$(_context) {
|
|
68
|
+
while (1) switch (_context.prev = _context.next) {
|
|
69
|
+
case 0:
|
|
70
|
+
config = _args.length > 0 && _args[0] !== undefined ? _args[0] : {};
|
|
71
|
+
_context.prev = 1;
|
|
72
|
+
if (ready) {
|
|
73
|
+
_context.next = 4;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
throw new Error("hCaptcha not ready");
|
|
77
|
+
case 4:
|
|
78
|
+
if (token) {
|
|
79
|
+
resetInstance();
|
|
80
|
+
}
|
|
81
|
+
_context.next = 7;
|
|
82
|
+
return hcaptchaRef.current.execute(_objectSpread({
|
|
83
|
+
async: true
|
|
84
|
+
}, config.rqdata ? {
|
|
85
|
+
rqdata: config.rqdata
|
|
86
|
+
} : {}));
|
|
87
|
+
case 7:
|
|
88
|
+
_yield$hcaptchaRef$cu = _context.sent;
|
|
89
|
+
response = _yield$hcaptchaRef$cu.response;
|
|
90
|
+
setToken(response);
|
|
91
|
+
return _context.abrupt("return", response);
|
|
92
|
+
case 13:
|
|
93
|
+
_context.prev = 13;
|
|
94
|
+
_context.t0 = _context["catch"](1);
|
|
95
|
+
setError(_context.t0);
|
|
96
|
+
onError && onError(_context.t0);
|
|
97
|
+
case 17:
|
|
98
|
+
case "end":
|
|
99
|
+
return _context.stop();
|
|
100
|
+
}
|
|
101
|
+
}, _callee, null, [[1, 13]]);
|
|
102
|
+
}));
|
|
103
|
+
return function executeInstance() {
|
|
104
|
+
return _ref2.apply(this, arguments);
|
|
105
|
+
};
|
|
106
|
+
}();
|
|
107
|
+
var resetInstance = function resetInstance() {
|
|
108
|
+
var _hcaptchaRef$current;
|
|
109
|
+
hcaptchaRef === null || hcaptchaRef === void 0 || (_hcaptchaRef$current = hcaptchaRef.current) === null || _hcaptchaRef$current === void 0 || _hcaptchaRef$current.resetCaptcha();
|
|
110
|
+
};
|
|
111
|
+
(0, _react.useEffect)(function () {
|
|
112
|
+
if (rqdata) {
|
|
113
|
+
var _hcaptchaRef$current2;
|
|
114
|
+
hcaptchaRef === null || hcaptchaRef === void 0 || (_hcaptchaRef$current2 = hcaptchaRef.current) === null || _hcaptchaRef$current2 === void 0 || _hcaptchaRef$current2.setData(rqdata);
|
|
115
|
+
}
|
|
116
|
+
}, [rqdata]);
|
|
117
|
+
return /*#__PURE__*/_react["default"].createElement(_Context.HCaptchaContext.Provider, {
|
|
118
|
+
value: {
|
|
119
|
+
sitekey: sitekey,
|
|
120
|
+
error: error,
|
|
121
|
+
token: token,
|
|
122
|
+
ready: ready,
|
|
123
|
+
executeInstance: executeInstance,
|
|
124
|
+
resetInstance: resetInstance
|
|
125
|
+
}
|
|
126
|
+
}, children, /*#__PURE__*/_react["default"].createElement(_index["default"], {
|
|
127
|
+
sitekey: sitekey,
|
|
128
|
+
size: size,
|
|
129
|
+
theme: theme,
|
|
130
|
+
languageOverride: languageOverride,
|
|
131
|
+
onReady: handleReady,
|
|
132
|
+
onVerify: handleVerify,
|
|
133
|
+
onExpire: handleExpire,
|
|
134
|
+
onError: handleError,
|
|
135
|
+
ref: hcaptchaRef
|
|
136
|
+
}));
|
|
137
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "HCaptchaProvider", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function get() {
|
|
9
|
+
return _Provider.HCaptchaProvider;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "useHCaptcha", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function get() {
|
|
15
|
+
return _useHCaptcha.useHCaptcha;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
var _useHCaptcha = require("./useHCaptcha.tsx");
|
|
19
|
+
var _Provider = require("./Provider.jsx");
|
package/package.json
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hcaptcha/react-hcaptcha",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.17.0",
|
|
4
4
|
"types": "types/index.d.ts",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/esm/index.js",
|
|
10
|
+
"require": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./hooks": {
|
|
13
|
+
"import": "./dist/esm/hooks/index.jsx",
|
|
14
|
+
"require": "./dist/hooks/index.jsx"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
7
17
|
"files": [
|
|
8
18
|
"src",
|
|
9
19
|
"dist",
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import React, { useEffect, useRef, useState } from "react";
|
|
2
|
+
import HCaptcha from "../index.js";
|
|
3
|
+
import { HCaptchaContext } from "./Context";
|
|
4
|
+
|
|
5
|
+
export const HCaptchaProvider = ({
|
|
6
|
+
sitekey = null,
|
|
7
|
+
size = "normal",
|
|
8
|
+
theme = "light",
|
|
9
|
+
rqdata = null,
|
|
10
|
+
languageOverride = null,
|
|
11
|
+
onVerify,
|
|
12
|
+
onError,
|
|
13
|
+
children,
|
|
14
|
+
}) => {
|
|
15
|
+
const hcaptchaRef = useRef(null);
|
|
16
|
+
|
|
17
|
+
const [ready, setReady] = useState(false);
|
|
18
|
+
const [token, setToken] = useState(null);
|
|
19
|
+
const [error, setError] = useState(null);
|
|
20
|
+
|
|
21
|
+
const handleReady = () => {
|
|
22
|
+
setReady(true);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const handleError = (error) => {
|
|
26
|
+
setError(error);
|
|
27
|
+
onError && onError(error);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const handleExpire = () => {
|
|
31
|
+
setToken(null);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const handleVerify = (token) => {
|
|
35
|
+
setToken(token);
|
|
36
|
+
onVerify && onVerify(token);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const executeInstance = async (config = {}) => {
|
|
40
|
+
try {
|
|
41
|
+
if (!ready) {
|
|
42
|
+
throw new Error("hCaptcha not ready");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (token) {
|
|
46
|
+
resetInstance();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const { response } = await hcaptchaRef.current.execute({
|
|
50
|
+
async: true,
|
|
51
|
+
...(config.rqdata ? { rqdata: config.rqdata } : {}),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
setToken(response);
|
|
55
|
+
|
|
56
|
+
return response;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
setError(error);
|
|
59
|
+
onError && onError(error);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const resetInstance = () => {
|
|
64
|
+
hcaptchaRef?.current?.resetCaptcha();
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
if (rqdata) {
|
|
69
|
+
hcaptchaRef?.current?.setData(rqdata);
|
|
70
|
+
}
|
|
71
|
+
}, [rqdata]);
|
|
72
|
+
|
|
73
|
+
return (
|
|
74
|
+
<HCaptchaContext.Provider
|
|
75
|
+
value={{
|
|
76
|
+
sitekey,
|
|
77
|
+
error,
|
|
78
|
+
token,
|
|
79
|
+
ready,
|
|
80
|
+
executeInstance,
|
|
81
|
+
resetInstance,
|
|
82
|
+
}}
|
|
83
|
+
>
|
|
84
|
+
{children}
|
|
85
|
+
<HCaptcha
|
|
86
|
+
sitekey={sitekey}
|
|
87
|
+
size={size}
|
|
88
|
+
theme={theme}
|
|
89
|
+
languageOverride={languageOverride}
|
|
90
|
+
onReady={handleReady}
|
|
91
|
+
onVerify={handleVerify}
|
|
92
|
+
onExpire={handleExpire}
|
|
93
|
+
onError={handleError}
|
|
94
|
+
ref={hcaptchaRef}
|
|
95
|
+
></HCaptcha>
|
|
96
|
+
</HCaptchaContext.Provider>
|
|
97
|
+
);
|
|
98
|
+
};
|