Files
ThreadNet-Web/src/components/structures/auth/ForgotPassword.tsx
T

367 lines
14 KiB
TypeScript
Raw Normal View History

2016-01-12 17:20:16 +00:00
/*
Copyright 2015, 2016 OpenMarket Ltd
2019-02-06 15:10:16 +00:00
Copyright 2017, 2018, 2019 New Vector Ltd
2019-08-23 18:43:55 +01:00
Copyright 2019 The Matrix.org Foundation C.I.C.
2016-01-12 17:20:16 +00: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.
*/
2017-10-27 01:23:50 +01:00
import React from 'react';
import { _t, _td } from '../../../languageHandler';
2019-12-19 18:19:56 -07:00
import * as sdk from '../../../index';
2017-10-27 01:23:50 +01:00
import Modal from "../../../Modal";
import PasswordReset from "../../../PasswordReset";
2021-06-29 13:11:58 +01:00
import AutoDiscoveryUtils, { ValidatedServerConfig } from "../../../utils/AutoDiscoveryUtils";
import classNames from 'classnames';
import AuthPage from "../../views/auth/AuthPage";
2020-10-29 15:53:14 +00:00
import CountlyAnalytics from "../../../CountlyAnalytics";
import ServerPicker from "../../views/elements/ServerPicker";
import PassphraseField from '../../views/auth/PassphraseField';
2021-06-29 13:11:58 +01:00
import { replaceableComponent } from "../../../utils/replaceableComponent";
import { PASSWORD_MIN_SCORE } from '../../views/auth/RegistrationForm';
2016-01-12 17:20:16 +00:00
2021-07-03 11:55:10 +01:00
import { IValidationResult } from "../../views/elements/Validation";
enum Phase {
// Show the forgot password inputs
Forgot = 1,
// Email is in the process of being sent
SendingEmail = 2,
// Email has been sent
EmailSent = 3,
// User has clicked the link in email and completed reset
Done = 4,
}
interface IProps {
serverConfig: ValidatedServerConfig;
2021-07-07 17:20:31 +02:00
onServerConfigChange: (serverConfig: ValidatedServerConfig) => void;
2021-07-03 11:55:10 +01:00
onLoginClick?: () => void;
onComplete: () => void;
}
interface IState {
phase: Phase;
email: string;
password: string;
password2: string;
errorText: string;
// We perform liveliness checks later, but for now suppress the errors.
// We also track the server dead errors independently of the regular errors so
// that we can render it differently, and override any other error the user may
// be seeing.
serverIsAlive: boolean;
serverErrorIsFatal: boolean;
serverDeadError: string;
passwordFieldValid: boolean;
}
2021-03-08 19:35:10 -07:00
@replaceableComponent("structures.auth.ForgotPassword")
2021-07-03 11:55:10 +01:00
export default class ForgotPassword extends React.Component<IProps, IState> {
private reset: PasswordReset;
2016-01-12 17:20:16 +00:00
2020-08-29 12:14:16 +01:00
state = {
2021-07-03 11:55:10 +01:00
phase: Phase.Forgot,
2020-08-29 12:14:16 +01:00
email: "",
password: "",
password2: "",
errorText: null,
2020-08-29 12:14:16 +01:00
// We perform liveliness checks later, but for now suppress the errors.
// We also track the server dead errors independently of the regular errors so
// that we can render it differently, and override any other error the user may
// be seeing.
serverIsAlive: true,
serverErrorIsFatal: false,
serverDeadError: "",
2021-07-03 11:55:10 +01:00
passwordFieldValid: false,
2020-08-29 12:14:16 +01:00
};
2016-01-12 17:20:16 +00:00
2021-07-03 11:55:10 +01:00
constructor(props: IProps) {
2020-10-29 15:53:14 +00:00
super(props);
CountlyAnalytics.instance.track("onboarding_forgot_password_begin");
}
2021-07-03 11:55:10 +01:00
public componentDidMount() {
2019-08-16 18:11:24 +01:00
this.reset = null;
2021-07-03 11:55:10 +01:00
this.checkServerLiveliness(this.props.serverConfig);
2020-08-29 12:14:16 +01:00
}
2020-04-01 14:35:39 -06:00
// TODO: [REACT-WARNING] Replace with appropriate lifecycle event
// eslint-disable-next-line
2021-07-03 11:55:10 +01:00
public UNSAFE_componentWillReceiveProps(newProps: IProps): void {
if (newProps.serverConfig.hsUrl === this.props.serverConfig.hsUrl &&
newProps.serverConfig.isUrl === this.props.serverConfig.isUrl) return;
// Do a liveliness check on the new URLs
2021-07-03 11:55:10 +01:00
this.checkServerLiveliness(newProps.serverConfig);
2020-08-29 12:14:16 +01:00
}
2021-07-03 11:55:10 +01:00
private async checkServerLiveliness(serverConfig): Promise<void> {
try {
await AutoDiscoveryUtils.validateServerConfigWithStaticUrls(
serverConfig.hsUrl,
serverConfig.isUrl,
);
2019-08-16 18:11:24 +01:00
this.setState({
serverIsAlive: true,
});
} catch (e) {
2021-07-03 11:55:10 +01:00
this.setState(AutoDiscoveryUtils.authComponentStateForError(e, "forgot_password") as IState);
}
2020-08-29 12:14:16 +01:00
}
2021-07-03 11:55:10 +01:00
public submitPasswordReset(email: string, password: string): void {
2016-01-12 17:20:16 +00:00
this.setState({
2021-07-03 11:55:10 +01:00
phase: Phase.SendingEmail,
2016-01-12 17:20:16 +00:00
});
this.reset = new PasswordReset(this.props.serverConfig.hsUrl, this.props.serverConfig.isUrl);
2019-11-18 10:03:05 +00:00
this.reset.resetPassword(email, password).then(() => {
2016-01-12 17:20:16 +00:00
this.setState({
2021-07-03 11:55:10 +01:00
phase: Phase.EmailSent,
2016-01-12 17:20:16 +00:00
});
}, (err) => {
this.showErrorDialog(_t('Failed to send email') + ": " + err.message);
2016-01-12 17:20:16 +00:00
this.setState({
2021-07-03 11:55:10 +01:00
phase: Phase.Forgot,
2016-01-12 17:20:16 +00:00
});
2017-01-20 14:22:27 +00:00
});
2020-08-29 12:14:16 +01:00
}
2016-01-12 17:20:16 +00:00
2021-07-03 11:55:10 +01:00
private onVerify = async (ev: React.MouseEvent): Promise<void> => {
2016-01-12 17:20:16 +00:00
ev.preventDefault();
if (!this.reset) {
console.error("onVerify called before submitPasswordReset!");
return;
}
2019-09-24 14:47:08 +01:00
try {
await this.reset.checkEmailLinkClicked();
2021-07-03 11:55:10 +01:00
this.setState({ phase: Phase.Done });
2019-09-24 14:47:08 +01:00
} catch (err) {
2016-01-12 17:20:16 +00:00
this.showErrorDialog(err.message);
2019-09-24 14:47:08 +01:00
}
2020-08-29 12:14:16 +01:00
};
2016-01-12 17:20:16 +00:00
2021-07-03 11:55:10 +01:00
private onSubmitForm = async (ev: React.FormEvent): Promise<void> => {
2016-01-12 17:20:16 +00:00
ev.preventDefault();
2019-06-11 10:29:00 +01:00
// refresh the server errors, just in case the server came back online
2021-07-03 11:55:10 +01:00
await this.checkServerLiveliness(this.props.serverConfig);
await this['password_field'].validate({ allowEmpty: false });
2016-01-12 17:20:16 +00:00
if (!this.state.email) {
this.showErrorDialog(_t('The email address linked to your account must be entered.'));
2017-10-11 17:56:17 +01:00
} else if (!this.state.password || !this.state.password2) {
this.showErrorDialog(_t('A new password must be entered.'));
} else if (!this.state.passwordFieldValid) {
this.showErrorDialog(_t('Please choose a strong password'));
2017-10-11 17:56:17 +01:00
} else if (this.state.password !== this.state.password2) {
this.showErrorDialog(_t('New passwords must match each other.'));
2017-10-11 17:56:17 +01:00
} else {
const QuestionDialog = sdk.getComponent("dialogs.QuestionDialog");
2017-07-27 17:19:18 +01:00
Modal.createTrackedDialog('Forgot Password Warning', '', QuestionDialog, {
2017-05-25 18:20:48 +01:00
title: _t('Warning!'),
description:
<div>
{ _t(
"Changing your password will reset any end-to-end encryption keys " +
2020-01-29 15:48:25 +00:00
"on all of your sessions, making encrypted chat history unreadable. Set up " +
"Key Backup or export your room keys from another session before resetting your " +
"password.",
) }
</div>,
button: _t('Continue'),
onFinished: (confirmed) => {
if (confirmed) {
this.submitPasswordReset(this.state.email, this.state.password);
}
},
});
2016-01-12 17:20:16 +00:00
}
2020-08-29 12:14:16 +01:00
};
2016-01-12 17:20:16 +00:00
2021-07-03 11:55:10 +01:00
private onInputChanged = (stateKey: string, ev: React.FormEvent<HTMLInputElement>) => {
2016-01-12 17:20:16 +00:00
this.setState({
2021-07-03 11:55:10 +01:00
[stateKey]: ev.currentTarget.value,
} as any);
2020-08-29 12:14:16 +01:00
};
2016-01-12 17:20:16 +00:00
2021-07-03 11:55:10 +01:00
private onLoginClick = (ev: React.MouseEvent): void => {
ev.preventDefault();
ev.stopPropagation();
this.props.onLoginClick();
2020-08-29 12:14:16 +01:00
};
2021-07-03 11:55:10 +01:00
public showErrorDialog(description: string, title?: string) {
2017-10-11 17:56:17 +01:00
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
2017-08-10 15:21:01 +01:00
Modal.createTrackedDialog('Forgot Password Error', '', ErrorDialog, {
2021-07-03 11:55:10 +01:00
title,
description,
2016-01-12 17:20:16 +00:00
});
2020-08-29 12:14:16 +01:00
}
2016-01-12 17:20:16 +00:00
2021-07-03 11:55:10 +01:00
private onPasswordValidate(result: IValidationResult) {
this.setState({
passwordFieldValid: result.valid,
});
}
2019-02-06 16:46:49 +00:00
renderForgot() {
2019-03-05 15:39:51 +00:00
const Field = sdk.getComponent('elements.Field');
2019-02-06 16:46:49 +00:00
let errorText = null;
const err = this.state.errorText;
2019-02-06 16:46:49 +00:00
if (err) {
errorText = <div className="mx_Login_error">{ err }</div>;
}
let serverDeadSection;
if (!this.state.serverIsAlive) {
const classes = classNames({
"mx_Login_error": true,
"mx_Login_serverError": true,
"mx_Login_serverErrorNonFatal": !this.state.serverErrorIsFatal,
});
serverDeadSection = (
<div className={classes}>
{ this.state.serverDeadError }
</div>
);
}
2019-02-06 16:46:49 +00:00
return <div>
{ errorText }
{ serverDeadSection }
<ServerPicker
serverConfig={this.props.serverConfig}
onServerConfigChange={this.props.onServerConfigChange}
/>
2019-02-06 16:46:49 +00:00
<form onSubmit={this.onSubmitForm}>
<div className="mx_AuthBody_fieldRow">
2019-03-05 15:39:51 +00:00
<Field
2019-02-06 16:46:49 +00:00
name="reset_email" // define a name so browser's password autofill gets less confused
2019-03-05 15:39:51 +00:00
type="text"
label={_t('Email')}
2019-02-06 16:46:49 +00:00
value={this.state.email}
onChange={this.onInputChanged.bind(this, "email")}
2019-03-05 15:39:51 +00:00
autoFocus
2020-10-29 15:53:14 +00:00
onFocus={() => CountlyAnalytics.instance.track("onboarding_forgot_password_email_focus")}
onBlur={() => CountlyAnalytics.instance.track("onboarding_forgot_password_email_blur")}
2019-03-05 15:39:51 +00:00
/>
2019-02-06 16:46:49 +00:00
</div>
<div className="mx_AuthBody_fieldRow">
<PassphraseField
2019-02-06 16:46:49 +00:00
name="reset_password"
2019-03-05 15:39:51 +00:00
type="password"
label={_td('New Password')}
2019-02-06 16:46:49 +00:00
value={this.state.password}
minScore={PASSWORD_MIN_SCORE}
2019-02-06 16:46:49 +00:00
onChange={this.onInputChanged.bind(this, "password")}
fieldRef={field => this['password_field'] = field}
onValidate={(result) => this.onPasswordValidate(result)}
2020-10-29 15:53:14 +00:00
onFocus={() => CountlyAnalytics.instance.track("onboarding_forgot_password_newPassword_focus")}
onBlur={() => CountlyAnalytics.instance.track("onboarding_forgot_password_newPassword_blur")}
2020-11-24 12:09:11 +00:00
autoComplete="new-password"
2019-03-05 15:39:51 +00:00
/>
<Field
2019-02-06 16:46:49 +00:00
name="reset_password_confirm"
2019-03-05 15:39:51 +00:00
type="password"
label={_t('Confirm')}
2019-02-06 16:46:49 +00:00
value={this.state.password2}
onChange={this.onInputChanged.bind(this, "password2")}
2020-10-29 15:53:14 +00:00
onFocus={() => CountlyAnalytics.instance.track("onboarding_forgot_password_newPassword2_focus")}
onBlur={() => CountlyAnalytics.instance.track("onboarding_forgot_password_newPassword2_blur")}
2020-11-24 12:09:11 +00:00
autoComplete="new-password"
2019-03-05 15:39:51 +00:00
/>
2019-02-06 16:46:49 +00:00
</div>
<span>{ _t(
2019-02-06 16:46:49 +00:00
'A verification email will be sent to your inbox to confirm ' +
'setting your new password.',
) }</span>
<input
className="mx_Login_submit"
type="submit"
value={_t('Send Reset Email')}
/>
2019-02-06 16:46:49 +00:00
</form>
<a className="mx_AuthBody_changeFlow" onClick={this.onLoginClick} href="#">
{ _t('Sign in instead') }
2019-02-06 16:46:49 +00:00
</a>
</div>;
2020-08-29 12:14:16 +01:00
}
2019-02-06 16:46:49 +00:00
renderSendingEmail() {
const Spinner = sdk.getComponent("elements.Spinner");
return <Spinner />;
2020-08-29 12:14:16 +01:00
}
2019-02-06 16:46:49 +00:00
renderEmailSent() {
return <div>
{ _t("An email has been sent to %(emailAddress)s. Once you've followed the " +
"link it contains, click below.", { emailAddress: this.state.email }) }
2019-02-06 16:46:49 +00:00
<br />
<input className="mx_Login_submit" type="button" onClick={this.onVerify}
value={_t('I have verified my email address')} />
</div>;
2020-08-29 12:14:16 +01:00
}
2019-02-06 16:46:49 +00:00
renderDone() {
return <div>
<p>{ _t("Your password has been reset.") }</p>
<p>{ _t(
2020-01-29 15:48:25 +00:00
"You have been logged out of all sessions and will no longer receive " +
"push notifications. To re-enable notifications, sign in again on each " +
2020-01-29 16:10:46 +00:00
"device.",
) }</p>
2019-02-06 16:46:49 +00:00
<input className="mx_Login_submit" type="button" onClick={this.props.onComplete}
value={_t('Return to login screen')} />
</div>;
2020-08-29 12:14:16 +01:00
}
2019-02-06 16:46:49 +00:00
2020-08-29 12:14:16 +01:00
render() {
const AuthHeader = sdk.getComponent("auth.AuthHeader");
2019-01-22 19:28:23 -06:00
const AuthBody = sdk.getComponent("auth.AuthBody");
2016-01-12 17:20:16 +00:00
2017-10-11 17:56:17 +01:00
let resetPasswordJsx;
2019-02-06 16:46:49 +00:00
switch (this.state.phase) {
2021-07-03 11:55:10 +01:00
case Phase.Forgot:
2019-02-06 16:46:49 +00:00
resetPasswordJsx = this.renderForgot();
break;
2021-07-03 11:55:10 +01:00
case Phase.SendingEmail:
2019-02-06 16:46:49 +00:00
resetPasswordJsx = this.renderSendingEmail();
break;
2021-07-03 11:55:10 +01:00
case Phase.EmailSent:
2019-02-06 16:46:49 +00:00
resetPasswordJsx = this.renderEmailSent();
break;
2021-07-03 11:55:10 +01:00
case Phase.Done:
2019-02-06 16:46:49 +00:00
resetPasswordJsx = this.renderDone();
break;
2016-01-12 17:20:16 +00:00
}
return (
<AuthPage>
<AuthHeader />
2019-01-22 19:28:23 -06:00
<AuthBody>
2019-01-23 15:03:43 -06:00
<h2> { _t('Set a new password') } </h2>
{ resetPasswordJsx }
2019-01-22 19:28:23 -06:00
</AuthBody>
</AuthPage>
2016-01-12 17:20:16 +00:00
);
2020-08-29 12:14:16 +01:00
}
}