Skip to content

Commit

Permalink
Fixed #1509 - Captcha throws a JS exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsincan committed Aug 13, 2020
1 parent ef3b4b4 commit 7457aef
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/components/captcha/Captcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ export class Captcha extends Component {
reset() {
if(this._instance === null)
return;

(window).grecaptcha.reset(this._instance);
}

getResponse() {
if (this._instance === null)
return null;

return (window).grecaptcha.getResponse(this._instance);
}

recaptchaCallback(response) {
if(this.props.onResponse) {
this.props.onResponse({
Expand All @@ -68,7 +68,7 @@ export class Captcha extends Component {
this.props.onExpire();
}
}

addRecaptchaScript() {
this.recaptchaScript = null;
if (!(window).grecaptcha) {
Expand All @@ -77,6 +77,16 @@ export class Captcha extends Component {
this.recaptchaScript.src = "https://www.google.com/recaptcha/api.js?render=explicit";
this.recaptchaScript.async = true;
this.recaptchaScript.defer = true;
this.recaptchaScript.onload = () => {
if (!(window).grecaptcha) {
console.warn("Recaptcha is not loaded");
return;
}

window.grecaptcha.ready(() => {
this.init();
});
}
head.appendChild(this.recaptchaScript);
}
}
Expand All @@ -85,16 +95,7 @@ export class Captcha extends Component {
this.addRecaptchaScript();

if ((window).grecaptcha) {
this.init();
}
else {
setTimeout(() => {
if (!(window).grecaptcha) {
console.warn("Recaptcha is not loaded");
return;
}
this.init();
},500);
this.init();
}
}

Expand All @@ -107,4 +108,4 @@ export class Captcha extends Component {
render() {
return <div id={this.props.id} ref={(el) => this.targetEL = ReactDOM.findDOMNode(el)}></div>
}
}
}

0 comments on commit 7457aef

Please sign in to comment.