2015-11-20 13:14:00 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-11-20 13:14:00 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-29 15:11:58 +03:00
|
|
|
import React, { createRef } from 'react';
|
2017-11-13 22:19:33 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2020-10-29 18:53:14 +03:00
|
|
|
import CountlyAnalytics from "../../../CountlyAnalytics";
|
2021-06-29 15:11:58 +03:00
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
2017-05-30 17:09:57 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const DIV_ID = 'mx_recaptcha';
|
2015-11-20 13:14:00 +03:00
|
|
|
|
2021-07-15 16:30:39 +03:00
|
|
|
interface IProps {
|
|
|
|
sitePublicKey?: string;
|
|
|
|
onCaptchaResponse: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
errorText: string;
|
|
|
|
}
|
|
|
|
|
2015-11-20 13:14:00 +03:00
|
|
|
/**
|
|
|
|
* A pure UI component which displays a captcha form.
|
|
|
|
*/
|
2021-03-09 05:45:39 +03:00
|
|
|
@replaceableComponent("views.auth.CaptchaForm")
|
2021-07-15 16:30:39 +03:00
|
|
|
export default class CaptchaForm extends React.Component<IProps, IState> {
|
|
|
|
private captchaWidgetId: string;
|
|
|
|
private recaptchaContainer = createRef<HTMLDivElement>();
|
2020-08-29 14:14:16 +03:00
|
|
|
static defaultProps = {
|
|
|
|
onCaptchaResponse: () => {},
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-10-11 20:00:47 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
this.state = {
|
2016-10-11 20:00:47 +03:00
|
|
|
errorText: null,
|
2015-11-20 13:14:00 +03:00
|
|
|
};
|
|
|
|
|
2020-10-29 18:53:14 +03:00
|
|
|
CountlyAnalytics.instance.track("onboarding_grecaptcha_begin");
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2017-06-16 12:25:51 +03:00
|
|
|
|
2021-07-15 16:30:39 +03:00
|
|
|
public componentDidMount(): void {
|
2015-11-20 13:14:00 +03:00
|
|
|
// Just putting a script tag into the returned jsx doesn't work, annoyingly,
|
|
|
|
// so we do this instead.
|
2021-07-15 16:30:39 +03:00
|
|
|
if (window.grecaptcha) { // TODO: Properly find the type of `grecaptcha`
|
2016-10-11 20:00:47 +03:00
|
|
|
// already loaded
|
2021-07-15 16:30:39 +03:00
|
|
|
this.onCaptchaLoaded();
|
2016-10-11 20:00:47 +03:00
|
|
|
} else {
|
2015-11-20 13:14:00 +03:00
|
|
|
console.log("Loading recaptcha script...");
|
2021-07-15 16:30:39 +03:00
|
|
|
window.mx_on_recaptcha_loaded = () => {this.onCaptchaLoaded();};
|
2019-02-19 16:57:31 +03:00
|
|
|
const scriptTag = document.createElement('script');
|
|
|
|
scriptTag.setAttribute(
|
2020-02-10 23:41:16 +03:00
|
|
|
'src', `https://www.recaptcha.net/recaptcha/api.js?onload=mx_on_recaptcha_loaded&render=explicit`,
|
2019-02-19 16:57:31 +03:00
|
|
|
);
|
2021-07-15 16:30:39 +03:00
|
|
|
this.recaptchaContainer.current.appendChild(scriptTag);
|
2015-11-20 13:14:00 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2015-11-20 13:14:00 +03:00
|
|
|
|
2021-07-15 16:30:39 +03:00
|
|
|
public componentWillUnmount(): void {
|
|
|
|
this.resetRecaptcha();
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2017-06-16 12:25:51 +03:00
|
|
|
|
2021-07-15 16:30:39 +03:00
|
|
|
private renderRecaptcha(divId): void {
|
|
|
|
if (!window.grecaptcha) {
|
2016-10-11 20:00:47 +03:00
|
|
|
console.error("grecaptcha not loaded!");
|
|
|
|
throw new Error("Recaptcha did not load successfully");
|
|
|
|
}
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
const publicKey = this.props.sitePublicKey;
|
2016-10-11 20:00:47 +03:00
|
|
|
if (!publicKey) {
|
|
|
|
console.error("No public key for recaptcha!");
|
|
|
|
throw new Error(
|
|
|
|
"This server has not supplied enough information for Recaptcha "
|
|
|
|
+ "authentication");
|
|
|
|
}
|
|
|
|
|
2019-11-26 04:14:03 +03:00
|
|
|
console.info("Rendering to %s", divId);
|
2021-07-15 16:30:39 +03:00
|
|
|
this.captchaWidgetId = window.grecaptcha.render(divId, {
|
2016-10-11 20:00:47 +03:00
|
|
|
sitekey: publicKey,
|
|
|
|
callback: this.props.onCaptchaResponse,
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-10-11 20:00:47 +03:00
|
|
|
|
2021-07-15 16:30:39 +03:00
|
|
|
private resetRecaptcha(): void {
|
|
|
|
if (this.captchaWidgetId !== null) {
|
|
|
|
window.grecaptcha.reset(this.captchaWidgetId);
|
2017-06-16 12:25:51 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2017-06-16 12:25:51 +03:00
|
|
|
|
2021-07-15 16:30:39 +03:00
|
|
|
private onCaptchaLoaded(): void {
|
2016-10-11 20:00:47 +03:00
|
|
|
console.log("Loaded recaptcha script.");
|
|
|
|
try {
|
2021-07-15 16:30:39 +03:00
|
|
|
this.renderRecaptcha(DIV_ID);
|
2020-11-11 17:00:40 +03:00
|
|
|
// clear error if re-rendered
|
|
|
|
this.setState({
|
|
|
|
errorText: null,
|
|
|
|
});
|
2020-10-29 18:53:14 +03:00
|
|
|
CountlyAnalytics.instance.track("onboarding_grecaptcha_loaded");
|
2016-10-11 20:00:47 +03:00
|
|
|
} catch (e) {
|
|
|
|
this.setState({
|
|
|
|
errorText: e.toString(),
|
2017-01-20 17:22:27 +03:00
|
|
|
});
|
2020-10-29 18:53:14 +03:00
|
|
|
CountlyAnalytics.instance.track("onboarding_grecaptcha_error", { error: e.toString() });
|
2016-10-11 20:00:47 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-10-11 20:00:47 +03:00
|
|
|
|
2021-07-15 16:30:39 +03:00
|
|
|
public render(): React.ReactNode {
|
2016-10-11 20:00:47 +03:00
|
|
|
let error = null;
|
|
|
|
if (this.state.errorText) {
|
|
|
|
error = (
|
|
|
|
<div className="error">
|
2017-10-11 19:56:17 +03:00
|
|
|
{ this.state.errorText }
|
2016-10-11 20:00:47 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-20 13:14:00 +03:00
|
|
|
return (
|
2021-07-15 16:30:39 +03:00
|
|
|
<div ref={this.recaptchaContainer}>
|
2019-02-19 16:59:15 +03:00
|
|
|
<p>{_t(
|
|
|
|
"This homeserver would like to make sure you are not a robot.",
|
|
|
|
)}</p>
|
2019-12-08 15:16:17 +03:00
|
|
|
<div id={DIV_ID} />
|
2017-10-11 19:56:17 +03:00
|
|
|
{ error }
|
2015-11-20 13:14:00 +03:00
|
|
|
</div>
|
|
|
|
);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|