2017-06-14 09:31:16 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2018-2024 New Vector Ltd.
|
2017-06-14 09:31:16 +01:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
|
|
|
|
|
2024-09-09 14:57:16 +01:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2017-06-14 09:31:16 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from "react";
|
2021-10-22 17:23:32 -05:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2023-04-14 09:40:19 -05:00
|
|
|
import { MatrixError } from "matrix-js-sdk/src/matrix";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2019-12-19 18:19:56 -07:00
|
|
|
import * as Email from "../../../email";
|
2017-06-14 09:31:16 +01:00
|
|
|
import AddThreepid from "../../../AddThreepid";
|
2023-04-14 09:40:19 -05:00
|
|
|
import { _t, UserFriendlyError } from "../../../languageHandler";
|
2017-06-14 09:31:16 +01:00
|
|
|
import Modal from "../../../Modal";
|
2021-09-05 12:42:18 +02:00
|
|
|
import Spinner from "../elements/Spinner";
|
2023-04-14 09:40:19 -05:00
|
|
|
import ErrorDialog, { extractErrorMessageFromError } from "./ErrorDialog";
|
2021-09-05 12:42:18 +02:00
|
|
|
import QuestionDialog from "./QuestionDialog";
|
|
|
|
|
import BaseDialog from "./BaseDialog";
|
|
|
|
|
import EditableText from "../elements/EditableText";
|
2023-05-30 10:36:34 +01:00
|
|
|
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
2021-09-05 12:42:18 +02:00
|
|
|
|
2023-02-28 10:31:48 +00:00
|
|
|
interface IProps {
|
2021-09-05 12:42:18 +02:00
|
|
|
title: string;
|
2023-02-28 10:31:48 +00:00
|
|
|
onFinished(ok?: boolean): void;
|
2021-09-05 12:42:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
|
emailAddress: string;
|
|
|
|
|
emailBusy: boolean;
|
|
|
|
|
}
|
2017-06-14 09:31:16 +01:00
|
|
|
|
2020-08-29 12:57:11 +01:00
|
|
|
/*
|
2017-06-14 09:31:16 +01:00
|
|
|
* Prompt the user to set an email address.
|
|
|
|
|
*
|
|
|
|
|
* On success, `onFinished(true)` is called.
|
|
|
|
|
*/
|
2021-09-05 12:42:18 +02:00
|
|
|
export default class SetEmailDialog extends React.Component<IProps, IState> {
|
2023-05-10 08:41:55 +01:00
|
|
|
private addThreepid?: AddThreepid;
|
2017-06-14 09:31:16 +01:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public constructor(props: IProps) {
|
2021-09-05 12:42:18 +02:00
|
|
|
super(props);
|
2017-06-14 09:31:16 +01:00
|
|
|
|
2021-09-05 12:42:18 +02:00
|
|
|
this.state = {
|
|
|
|
|
emailAddress: "",
|
|
|
|
|
emailBusy: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private onEmailAddressChanged = (value: string): void => {
|
2017-06-14 09:31:16 +01:00
|
|
|
this.setState({
|
|
|
|
|
emailAddress: value,
|
|
|
|
|
});
|
2020-08-29 12:14:16 +01:00
|
|
|
};
|
2017-06-14 09:31:16 +01:00
|
|
|
|
2021-09-05 12:42:18 +02:00
|
|
|
private onSubmit = (): void => {
|
2017-06-14 09:31:16 +01:00
|
|
|
const emailAddress = this.state.emailAddress;
|
|
|
|
|
if (!Email.looksValid(emailAddress)) {
|
2022-06-14 17:51:51 +01:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
2023-09-26 18:35:55 +01:00
|
|
|
title: _t("settings|general|error_invalid_email"),
|
|
|
|
|
description: _t("settings|general|error_invalid_email_detail"),
|
2017-06-14 09:31:16 +01:00
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-06-15 08:46:19 +01:00
|
|
|
this.addThreepid = new AddThreepid(MatrixClientPeg.safeGet());
|
2021-09-05 12:42:18 +02:00
|
|
|
this.addThreepid.addEmailAddress(emailAddress).then(
|
|
|
|
|
() => {
|
2022-06-14 17:51:51 +01:00
|
|
|
Modal.createDialog(QuestionDialog, {
|
2023-10-03 19:17:26 +01:00
|
|
|
title: _t("auth|set_email|verification_pending_title"),
|
|
|
|
|
description: _t("auth|set_email|verification_pending_description"),
|
2023-08-22 20:55:15 +01:00
|
|
|
button: _t("action|continue"),
|
2017-06-14 09:31:16 +01:00
|
|
|
onFinished: this.onEmailDialogFinished,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
(err) => {
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ emailBusy: false });
|
2021-10-15 16:30:53 +02:00
|
|
|
logger.error("Unable to add email address " + emailAddress + " " + err);
|
2022-06-14 17:51:51 +01:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
2023-09-26 18:35:55 +01:00
|
|
|
title: _t("settings|general|error_add_email"),
|
2023-09-22 16:39:40 +01:00
|
|
|
description: extractErrorMessageFromError(err, _t("invite|failed_generic")),
|
2017-06-14 09:31:16 +01:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ emailBusy: true });
|
2020-08-29 12:14:16 +01:00
|
|
|
};
|
2017-06-14 09:31:16 +01:00
|
|
|
|
2021-09-05 12:42:18 +02:00
|
|
|
private onCancelled = (): void => {
|
2017-06-14 14:58:39 +01:00
|
|
|
this.props.onFinished(false);
|
2020-08-29 12:14:16 +01:00
|
|
|
};
|
2017-06-14 14:58:39 +01:00
|
|
|
|
2021-09-05 12:42:18 +02:00
|
|
|
private onEmailDialogFinished = (ok: boolean): void => {
|
2017-06-14 09:31:16 +01:00
|
|
|
if (ok) {
|
|
|
|
|
this.verifyEmailAddress();
|
|
|
|
|
} else {
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ emailBusy: false });
|
2017-06-14 09:31:16 +01:00
|
|
|
}
|
2020-08-29 12:14:16 +01:00
|
|
|
};
|
2017-06-14 09:31:16 +01:00
|
|
|
|
2021-09-05 12:42:18 +02:00
|
|
|
private verifyEmailAddress(): void {
|
2023-05-10 08:41:55 +01:00
|
|
|
this.addThreepid?.checkEmailLinkClicked().then(
|
2021-09-05 12:42:18 +02:00
|
|
|
() => {
|
2017-06-14 09:31:16 +01:00
|
|
|
this.props.onFinished(true);
|
|
|
|
|
},
|
|
|
|
|
(err) => {
|
2021-06-29 13:11:58 +01:00
|
|
|
this.setState({ emailBusy: false });
|
2023-04-14 09:40:19 -05:00
|
|
|
|
|
|
|
|
let underlyingError = err;
|
|
|
|
|
if (err instanceof UserFriendlyError) {
|
|
|
|
|
underlyingError = err.cause;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (underlyingError instanceof MatrixError && underlyingError.errcode === "M_THREEPID_AUTH_FAILED") {
|
2017-06-14 09:31:16 +01:00
|
|
|
const message =
|
2023-09-26 18:35:55 +01:00
|
|
|
_t("settings|general|error_email_verification") +
|
2017-06-14 09:31:16 +01:00
|
|
|
" " +
|
2023-10-03 19:17:26 +01:00
|
|
|
_t("auth|set_email|verification_pending_description");
|
2022-06-14 17:51:51 +01:00
|
|
|
Modal.createDialog(QuestionDialog, {
|
2023-10-03 19:17:26 +01:00
|
|
|
title: _t("auth|set_email|verification_pending_title"),
|
2017-06-14 09:31:16 +01:00
|
|
|
description: message,
|
2023-08-22 20:55:15 +01:00
|
|
|
button: _t("action|continue"),
|
2017-06-14 09:31:16 +01:00
|
|
|
onFinished: this.onEmailDialogFinished,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2021-10-15 16:30:53 +02:00
|
|
|
logger.error("Unable to verify email address: " + err);
|
2022-06-14 17:51:51 +01:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
2023-09-26 18:35:55 +01:00
|
|
|
title: _t("settings|general|error_email_verification"),
|
2023-09-22 16:39:40 +01:00
|
|
|
description: extractErrorMessageFromError(err, _t("invite|failed_generic")),
|
2017-06-14 09:31:16 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
2017-06-14 09:31:16 +01:00
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2017-06-14 09:31:16 +01:00
|
|
|
const emailInput = this.state.emailBusy ? (
|
|
|
|
|
<Spinner />
|
|
|
|
|
) : (
|
|
|
|
|
<EditableText
|
2018-06-18 18:17:10 +01:00
|
|
|
initialValue={this.state.emailAddress}
|
2017-06-14 09:31:16 +01:00
|
|
|
className="mx_SetEmailDialog_email_input"
|
2023-10-03 19:17:26 +01:00
|
|
|
placeholder={_t("common|email_address")}
|
2017-06-14 09:31:16 +01:00
|
|
|
placeholderClassName="mx_SetEmailDialog_email_input_placeholder"
|
2017-09-28 11:21:06 +01:00
|
|
|
blurToCancel={false}
|
|
|
|
|
onValueChanged={this.onEmailAddressChanged}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2017-06-14 09:31:16 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<BaseDialog
|
|
|
|
|
className="mx_SetEmailDialog"
|
2017-06-14 14:58:39 +01:00
|
|
|
onFinished={this.onCancelled}
|
2017-06-14 09:31:16 +01:00
|
|
|
title={this.props.title}
|
2017-12-05 13:52:20 +01:00
|
|
|
contentId="mx_Dialog_content"
|
2017-06-14 09:31:16 +01:00
|
|
|
>
|
|
|
|
|
<div className="mx_Dialog_content">
|
2023-10-03 19:17:26 +01:00
|
|
|
<p id="mx_Dialog_content">{_t("auth|set_email|description")}</p>
|
2017-06-14 09:31:16 +01:00
|
|
|
{emailInput}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="mx_Dialog_buttons">
|
2023-08-22 20:55:15 +01:00
|
|
|
<input
|
|
|
|
|
className="mx_Dialog_primary"
|
|
|
|
|
type="submit"
|
|
|
|
|
value={_t("action|continue")}
|
|
|
|
|
onClick={this.onSubmit}
|
|
|
|
|
/>
|
2023-08-23 11:57:22 +01:00
|
|
|
<input type="submit" value={_t("action|skip")} onClick={this.onCancelled} />
|
2017-06-14 09:31:16 +01:00
|
|
|
</div>
|
|
|
|
|
</BaseDialog>
|
|
|
|
|
);
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
|
|
|
|
}
|