Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement async hcaptcha execute #91

Merged
merged 19 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"presets": [
"@babel/env",
"@babel/react"
]
],
"plugins": [["@babel/transform-runtime", {
"regenerator": true
}]]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ In these instances, you'll most likely want to use `ref` to handle the callbacks

|Method|Description|
|---|---|
|`execute()`|Programmatically trigger a challenge request|
|`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.|
|`resetCaptcha()`|Reset the current challenge|


Expand Down
96 changes: 68 additions & 28 deletions examples/src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
const { useRef } = require('react');
const React = require('react');
const {render} = require('react-dom');
const HCaptcha = require('../../dist/');

const AsyncDemo = () => {
const captchaRef = useRef();

const executeCaptcha = async () => {
try {
const res = await captchaRef.current.execute();
console.log("Verified asynchronously: ", res);

} catch (error) {
console.log(error);
}
}

return (
<div>
<HCaptcha ref={captchaRef}
sitekey="917ba1eb-0b37-486e-9c90-39f3cb7b2579"
theme="light"
onVerify={() => undefined}
/>
<button onClick={executeCaptcha}>Execute asynchronously</button>
</div>
);
}

class ReactDemo extends React.Component {

constructor(props) {
super(props);

this.state = {isVerified: false};
this.state = {isVerified: false, async: false};
this.captcha = React.createRef();

this.handleChange = this.handleChange.bind(this);
Expand Down Expand Up @@ -46,35 +72,49 @@ class ReactDemo extends React.Component {
<p>
Set your sitekey and onVerify callback as props, and drop into your form. From here, we'll take care of the rest.
</p>
<div>
<HCaptcha ref={this.captcha} onVerify={this.onVerifyCaptcha} languageOverride={this.languageOverride}
sitekey="917ba1eb-0b37-486e-9c90-39f3cb7b2579"
theme="light"
/>
</div>

<div>
<HCaptcha ref={this.captcha} onVerify={this.onVerifyCaptcha} languageOverride={this.languageOverride}
sitekey="917ba1eb-0b37-486e-9c90-39f3cb7b2579"
theme="dark"
/>
</div>

<div>
<HCaptcha ref={this.captcha} onVerify={this.onVerifyCaptcha} languageOverride={this.languageOverride}
sitekey="917ba1eb-0b37-486e-9c90-39f3cb7b2579"
size="compact"
theme="dark"
/>
<div style={{marginBottom: 10}}>
<label>
<input type="radio" name="behavior" checked={!this.state.async} onChange={() => this.setState({ async: false })}></input>
Normal
</label>
<label>
<input type="radio" name="behavior" checked={this.state.async} onChange={() => this.setState({ async: true })}></input>
Asynchronous
</label>
</div>
{!this.state.async ? (
<>
<div>
<HCaptcha ref={this.captcha} onVerify={this.onVerifyCaptcha} languageOverride={this.languageOverride}
sitekey="917ba1eb-0b37-486e-9c90-39f3cb7b2579"
theme="light"
/>
</div>

{isVerified &&
<div>
<p>Open your console to see the Verified response.</p>
<button onClick={this.handleReset}>Reset Captcha</button>
</div>
}
<div>
<HCaptcha ref={this.captcha} onVerify={this.onVerifyCaptcha} languageOverride={this.languageOverride}
sitekey="917ba1eb-0b37-486e-9c90-39f3cb7b2579"
theme="dark"
/>
</div>

<div>
<HCaptcha ref={this.captcha} onVerify={this.onVerifyCaptcha} languageOverride={this.languageOverride}
sitekey="917ba1eb-0b37-486e-9c90-39f3cb7b2579"
size="compact"
theme="dark"
/>
</div>
{isVerified &&
<div>
<p>Open your console to see the Verified response.</p>
<button onClick={this.handleReset}>Reset Captcha</button>
</div>
}
</>
) : (
<AsyncDemo />
)}
</div>
);
}
Expand All @@ -83,7 +123,7 @@ class ReactDemo extends React.Component {
render(
<div>
<h1>HCaptcha React Demo</h1>
<ReactDemo/>
<ReactDemo />
</div>,
document.getElementById('app')
);
Loading